microsoft / pxt-arcade

Arcade game editor based on Microsoft MakeCode
https://arcade.makecode.com
MIT License
477 stars 205 forks source link

game simulator stuck keyboard bug #2327

Open tballmsft opened 4 years ago

tballmsft commented 4 years ago

Sometimes when I am using the game simulator with the keyboard (specifically, the arrow keys) and doing a lot of quick keystrokes (say for moving a character), one (or sometimes two) of the keys will start repeating even though I am not pressing the keys. This happens non-deterministically and basically destroys game play. The only recourse is to restart the game simulator.

tballmsft commented 4 years ago

On both the Arcade simulator and hardware, I code the following

However, sometimes the REPEAT events keep coming, even though I have release the left-arrow key.

I have traced this problem down to an inversion of handlers. In both the browser and Arcade devices, the act of holding down the left-button continuously generates KEY_DOWN events. What I am seeing in the trace though is the following

  1. Long trace of KEY_DOWN events and KEY_REPEAT generated by controller
  2. last KEY_DOWN event
  3. last KEY_UP event
  4. KEY_UP_handler invoked
  5. KEY_DOWN handler invoked

The important part is the inversion of handler invocation that happens (this is the race condition): the KEY_UP handler is invoked before the KEY_DOWN handler even though the KEY_UP event occurred after the KEY_DOWN event – the result is that controller logic keeps generating KEY_REPEAT events, as the last event received was KEY_DOWN.

The usual and correct trace is:

  1. Long trace of KEY_DOWN events and KEY_REPEAT generated by controller
  2. last KEY_DOWN event
  3. KEY_DOWN handler invoked
  4. KEY_UP_handler invoked
  5. last KEY_UP event

Each handler is execute by a different fiber. Given sequential code that spawns two threads, P and Q, in sequence, it certainly seems like it should be possible for the code of Q to start executing before that of P. Is the same thing possible with fibers?