Fix RDP client disposal race condition causing Microsoft Store version crashes (#937)#1008
Merged
Fix RDP client disposal race condition causing Microsoft Store version crashes (#937)#1008
Conversation
Changed RdpClientDispose to use synchronous disposal (Execute.OnUIThreadSync) instead of async (Execute.OnUIThread) to prevent race conditions. Also added thread-safe locking in DisposeRdpClient to prevent double disposal. Fixes #937 Co-authored-by: VShawn <10143738+VShawn@users.noreply.github.com>
Address code review feedback by introducing _rdpClientDisposeLock instead of using lock(this), which can lead to deadlocks. Updated all methods that access _rdpClient to use the same lock object for consistency. Co-authored-by: VShawn <10143738+VShawn@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix issue 937 and ensure functionality remains consistent
Fix RDP client disposal race condition causing Microsoft Store version crashes (#937)
Oct 19, 2025
VShawn
approved these changes
Oct 19, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Issue #937 reported that the Microsoft Store version 1.2.0 crashes after RDP connections, while the sideloaded package works correctly. Investigation revealed a critical race condition in the RDP client disposal logic that was exposed more frequently by the Microsoft Store version's threading characteristics.
Root Cause
The
RdpClientDispose()method inAxMsRdpClient09Host.xaml.csused asynchronous disposal viaExecute.OnUIThread(), which allowed code execution to continue before the RDP client was fully disposed. This created several race conditions:Race condition scenarios:
ReConn()callsRdpClientDispose()then immediately attempts to create a new RDP connection before the old client is fully disposedRdpClientDispose()then invokesOnClosedcallback before disposal completesThe log message "RDP Host: _rdpClient.Disposed." was printing before actual disposal completed, masking the timing issue.
Solution
Changed to synchronous disposal to ensure the RDP client is fully disposed before any subsequent operations:
Additionally, introduced a dedicated lock object
_rdpClientDisposeLockto ensure thread-safe access during creation and disposal, replacing thelock(this)pattern which can lead to deadlocks.Changes
Execute.OnUIThread()toExecute.OnUIThreadSync()inRdpClientDispose()for synchronous disposalprivate readonly object _rdpClientDisposeLockfor thread-safe disposalDisposeRdpClient(),CreateRdpClient(), andOnScreenResolutionChanged()to use the dedicated lockImpact
These minimal, surgical changes ensure:
Testing
Due to the WPF/Windows-specific nature of this code, testing requires a Windows environment. Recommended test scenarios:
Fixes #937
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.