onPoll and onChangeRequest interaction

I’m trying to put the finishing touches on a plugin and have run into some odd behavior. I have an onPoll() that runs every 5 seconds or so and it makes a number of queries to the device to get various pieces of state information. That works fine. I have guarded these queries with a gettingStatus flag that is a global variable in the plugin, so that when getting the status, that variable is true. I did this because in onChangeRequest() I also send commands to the device. The device, unfortunately, has a brain-dead protocol and things get out of whack if a command is sent before another one completes. onChangeRequest() checks the gettingStatus flag (actually spins in a while loop with a sleep() call in it) before sending whatever command was triggered. If the button that triggers onChangeRequest() is pressed while no status is being pulled, everything works fine.

However, if onPoll() is in the middle of making its queries (and gettingStatus is set to true) and a button is pressed triggering onChangeRequest(), onChangeRequest() goes into its while loop. So far, so good. The problem is that onPoll() stops executing/processing. The consequence is that gettingStatus will never be set back to false because onPoll() ever completes, and the 2 methods deadlock.

I’ve never really encountered an issue like this before–I’ve had polling status checks that worked fine with button presses, but I’m guessing those devices were just not as sensitive to the only-one-at-a-time situation, as I’ve never had to implement a guard like this before. Fundamentally, I guess my question is, how do I give processing control back to onPoll() in onChangeRequest() so it can finish? I can understand why a change event (such as a button press sending a MediaCommand) would want to preempt onPoll() for responsiveness reasons, but I’ve got to let it finish.

Any thoughts are greatly appreciated here and happy to provide more details.