Skip to content

Fix RDP client disposal race condition causing Microsoft Store version crashes (#937)#1008

Merged
VShawn merged 3 commits intomainfrom
copilot/fix-issue-937-and-verify
Oct 19, 2025
Merged

Fix RDP client disposal race condition causing Microsoft Store version crashes (#937)#1008
VShawn merged 3 commits intomainfrom
copilot/fix-issue-937-and-verify

Conversation

Copy link
Contributor

Copilot AI commented Oct 19, 2025

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 in AxMsRdpClient09Host.xaml.cs used asynchronous disposal via Execute.OnUIThread(), which allowed code execution to continue before the RDP client was fully disposed. This created several race conditions:

// Before fix - ASYNC disposal
Execute.OnUIThread(DisposeRdpClient);  // Schedules disposal asynchronously
// Code continues immediately, before disposal completes!

Race condition scenarios:

  1. Reconnection race: ReConn() calls RdpClientDispose() then immediately attempts to create a new RDP connection before the old client is fully disposed
  2. Disconnect callback race: Disconnect handler calls RdpClientDispose() then invokes OnClosed callback before disposal completes
  3. Double disposal race: Multiple threads could attempt to dispose the client simultaneously without proper synchronization

The 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:

// After fix - SYNC disposal
Execute.OnUIThreadSync(DisposeRdpClient);  // Waits for disposal to complete
// Code continues only after disposal is done

Additionally, introduced a dedicated lock object _rdpClientDisposeLock to ensure thread-safe access during creation and disposal, replacing the lock(this) pattern which can lead to deadlocks.

Changes

  • Changed Execute.OnUIThread() to Execute.OnUIThreadSync() in RdpClientDispose() for synchronous disposal
  • Added private readonly object _rdpClientDisposeLock for thread-safe disposal
  • Updated DisposeRdpClient(), CreateRdpClient(), and OnScreenResolutionChanged() to use the dedicated lock

Impact

These minimal, surgical changes ensure:

  • RDP client is fully disposed before reconnection attempts or callbacks execute
  • Thread-safe disposal prevents concurrent access crashes
  • Microsoft Store version should have the same stability as the sideloaded version
  • No API or behavior changes for existing functionality

Testing

Due to the WPF/Windows-specific nature of this code, testing requires a Windows environment. Recommended test scenarios:

  • RDP reconnection (disconnect and reconnect)
  • Rapid connection/disconnection cycles
  • Alternate address switching
  • Verify no regressions in normal RDP operations

Fixes #937

Original prompt
  1. https://github.com/1Remote/1Remote/issues/937,阅读其中的讨论和相关issue和代码版本,分析问题现象和原因,并尝试解决。
  2. 如果能够正确解决该问题,修复所有涉及到的代码,并在修复后检查确认修改前后功能要保持一致。

💡 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.

Copilot AI and others added 2 commits October 19, 2025 04:01
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
Copilot AI requested a review from VShawn October 19, 2025 04:11
@VShawn VShawn marked this pull request as ready for review October 19, 2025 09:51
@VShawn VShawn merged commit ff94a19 into main Oct 19, 2025
4 checks passed
@VShawn VShawn deleted the copilot/fix-issue-937-and-verify branch October 19, 2025 09:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1.2.0 encountered a malfunction in the Microsoft Store.

2 participants