Skip to content

Port pinvoke-detach tests to Windows - #138004

Open
Samyra312007 wants to merge 1 commit into
dotnet:mainfrom
Samyra312007:mono-pinvoke-detach-windows-fix
Open

Samyra312007 wants to merge 1 commit into
dotnet:mainfrom
Samyra312007:mono-pinvoke-detach-windows-fix

Conversation

@Samyra312007

Copy link
Copy Markdown

Fixes #43093

Port the pinvoke-detach-1.cs tests to Windows

Added a Windows implementation for the runtime‑attach & detach tests that were previously disabled by a ActiveIssue marker. The native library mono-embedding-api-test and the vendored test library libtest.c now provide thread‑creation and coordination logic that mirrors the POSIX behavior using CreateThread, CreateEvent, CreateMutex and WaitForSingleObject.

Removed the [ActiveIssue] that prevented the test from running on Windows.


Context

The pinvoke-detach-1.cs test exercises the Mono embedding API when a foreign thread attaches to the runtime. The test had an ActiveIssue that skipped it on Windows, making it impossible to verify core correctness on that platform.
This patch restores the test for Mono on Windows, ensuring the runtime can detach threads cleanly and shut down even when a foreign thread remains blocked after invoking into the runtime.


Implementation Details

  • Win32 API usage – Replaced pthread_create, pthread_cond_*, and pthread_mutex_* with CreateThread, CreateEvent, CreateMutex, and WaitForSingleObject.
  • Thread coordination – An event signals the test thread that the foreign thread has called into the runtime.
  • Blocking logic – A mutex locked once and never released emulates the POSIX “block forever” behavior.
  • Code layout – The #else branch in both mono-embedding-api-test/mono-embedding-api-test.c and mono/mono/tests/libtest.c contains identical logic.

Tests

  • PInvokeDetach C# test now runs on Windows and succeeds (previously blocked due to ActiveIssue).
  • Reference libtest.c compiles on both Windows and Linux and is exercised by the existing test suite.
  • All other tests continue to pass on all platforms.

Dependencies

None. The change uses only the standard Win32 API available on Visual Studio 2019+.


Risk & Reversal

  • Only a few lines of Win32 code were added; the logic is a faithful port of the original POSIX implementation and mirrors the reference libtest.c.
  • Non‑Windows builds remain unaffected.
  • If a future Win‑build fails, the #else block can be removed to revert to the original POSIX logic.

Review Checklist

  • Commit message conforms to the short description + body convention.
  • Builds and runs on all relevant platforms.
  • Existing tests still pass on non‑Windows platforms.
  • The ActiveIssue removal is justified by the new Windows implementation.
  • No new race conditions or deadlocks introduced.

Proposed Reviewers

  • @dotnet/runtime-core
  • @dotnet/runtime-docs (for any documentation updates)

Notes

None


Add Windows implementations of mono_test_attach_invoke_foreign_thread
and mono_test_attach_invoke_block_foreign_thread in both the native
embedding-api-test library and the vendored libtest.c, using CreateThread,
CreateEvent/CreateMutex and WaitForSingleObject to mirror the POSIX
pthread/condvar coordination. Remove the ActiveIssue that disabled the
test on Windows for Mono.
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 20, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@huoyaoyuan

Copy link
Copy Markdown
Member

Thank you for your interest of contribution. We are going to sunset mono in this version, and being removing mono related tests (#137906). It's no longer meaningful to add or port tests, unless CoreCLR gets the same feature (#67047).

@Samyra312007

Copy link
Copy Markdown
Author

Thank you for your interest of contribution. We are going to sunset mono in this version, and being removing mono related tests (#137906). It's no longer meaningful to add or port tests, unless CoreCLR gets the same feature (#67047).

Thanks for informing.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-VM-meta-mono community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[mono] Implement pinvoke-detach-1.cs tests on Windows

2 participants