I've been getting exceptions of type System.Collections.Generic.KeyNotFoundException from ResponseManager.HandleResponse. From my reading of the code, this means that a response with a specific sequence number was not found in the Responses collection.
Have you seen anything like this - any idea how this could happen?
Also, while I was trying to understand this code, one example of the path looks like this :
- Brick.PollSensorsAsync creates a new Command of DirectReply type. This also creates a Response object for the command which is stored in the Responses collection. The command is sent off using SendCommandAsyncInternal on which PollSensorsAsync blocks.
- Brick.SendCommandAsyncInternal sends the command and calls ResponseManager.WaitForResponseAsync to block for the response.
- If the response comes in (through the ICommunication object), ResponseManager.HandleResponse handles it and signals the response's Event so that ResponseManager.WaitForResponseAsync competes, and the response is removed from the Responses collection.
- If no response is received within a second, the reply type is set to error, but the response is not removed - in fact it does not look like it is ever removed.
Is the behavior in 4 above normal in the error case, i.e. the response is not removed?
Is it possible for there to be a race condition between WaitForResponseAsync timing out on WaitOne and the response being handled?
I still can't explain the exception that I'm seeing, but is it possible that there is a race between the polling task and some other command (like one to apply power to the motors), such that both threads try to call ResponseManager.CreateResonse at the same
time? Is incrementing the sequence number atomic or exclusive?