Skip to content

Commit

Permalink
LukeBotClient: Fix incorrect state of await response event
Browse files Browse the repository at this point in the history
Additionally CommandExecutor now interrupts its worker thread on exit,
to prevent a deadlock in case of disposing it.
  • Loading branch information
lookeypl committed Oct 30, 2024
1 parent b9ded67 commit 9ff195d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
3 changes: 2 additions & 1 deletion LukeBot/CommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ public void Execute(CommandDelegate command)

public void Dispose()
{
Logger.Log().Debug("Dispose");
Logger.Log().Debug("Dispose; Thread state = {0}", mWorkerThread.ThreadState);
mWorkerDone = true;
mCommandAvailableEvent.Set();
mWorkerThread.Interrupt();
mWorkerThread.Join();
}
}
Expand Down
8 changes: 2 additions & 6 deletions LukeBotClient/LukeBotClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ enum State
private Queue<string> mRecvQueue = new();
private bool mRecvThreadDone = false;
private Mutex mPrintMutex = new();
private State mState = State.Init;
private ManualResetEvent mAwaitResponseEvent = new(true);
private volatile State mState = State.Init;
private ManualResetEvent mAwaitResponseEvent = new(false);
private IntPtr mMainThreadStdinHandle = IntPtr.Zero;
private const string PROMPT_SUFFIX = "> ";
private string mCurrentPrompt = "";
Expand Down Expand Up @@ -184,8 +184,6 @@ public async void ReceiveThreadMain()
}
case ServerMessageType.Query:
{
PrintLine("state = " + mState);

if (mState != State.AwaitingResponse)
{
PrintLine("WARNING - Queries should only be received when awaiting a response to a Command");
Expand All @@ -203,8 +201,6 @@ public async void ReceiveThreadMain()
answer = Query(m.MaskAnswer, m.Query);
}

PrintLine("answer is " + answer);

QueryResponseServerMessage r = new(m, answer);
await SendObject(r);
break;
Expand Down

0 comments on commit 9ff195d

Please sign in to comment.