monome / crow

Crow speaks and listens and remembers bits of text. A scriptable USB-CV-II machine
GNU General Public License v3.0
166 stars 34 forks source link

simultaneous use of teletype queries and norns crow.send crashes crow #492

Open Dewb opened 9 months ago

Dewb commented 9 months ago

When simultaneously receiving serial messages from norns and i2c query commands from Teletype, crow will eventually crash.

Crow can also be made to crash immediately by redefining the ii.self.query0 function while this is happening.

Example real-world application: adding an option to dreamsequence to synchronize its scale with teletype (branch)

The crash is observable in both crow 3.0.0 and crow 4.0.5.

Example repro script (this is not totally minimal, I believe one coroutine calling crow.send is sufficient, but more crash sooner.)

-- minimal script to reproduce teletype-crow-norns crash issue
-- steps to reproduce:
--   connect crow to teletype via i2c
--   put N.B CROW.Q0 in M script (default M 1000 is sufficient, but faster Ms crash sooner)
--   connect crow to norns via usb
--   run this script
--   crow will generally crash within 20-30 seconds. norns is hung until crow is disconnected or power cycled.

crow_updates = {}

function init()
  redraw()

  clock.run(function() 
    while true do
      update_crow(1)
      clock.sleep(0.5)
    end
  end)

  clock.run(function() 
    while true do
      update_crow(2)
      clock.sleep(0.17)
    end
  end)

  -- uncomment this to crash immediately  
  -- clock.run(function()
  --   clock.sleep(1)
  --   crow.send("ii.self.query0 = function() print('query0*') end")
  -- end)
end

function redraw()
  screen.clear()
  screen.level(15)
  screen.move(2,8)
  screen.text("crow stress test " .. os.clock())

  for i = 1, #crow_updates do
    if crow_updates[i] then
      screen.move(12, 20 + (i-1) * 12)
      screen.text("ran crow update " .. i)
      crow_updates[i] = false
    end
  end

  screen.update()
end

function refresh()
  redraw()
  update_crow(3)
end

function update_crow(n)
  norns.crow.send("foo = " .. n)
  crow_updates[n] = true
end
Dewb commented 9 months ago

I had a chance to look at the i2c code and I see that based on the danger/fixme comments that this isn’t expected to work yet. I’d be happy to work on a PR to make the i2c queries safe, but I can imagine several different possible strategies with different tradeoffs, did anyone already have a plan in mind?