Windows: release VirtualVector reservations only on FreeLibrary, not at process exit - #886
Merged
Matthew Parkinson (mjp41) merged 3 commits intoOct 1, 2026
Conversation
Co-authored-by: mjp41 <270363+mjp41@users.noreply.github.com>
Co-authored-by: mjp41 <270363+mjp41@users.noreply.github.com>
Co-authored-by: mjp41 <270363+mjp41@users.noreply.github.com>
Member
|
Damoy (@DamoyY) would you be able to confirm that this PR fixes the issue for you. |
|
Matthew Parkinson (@mjp41) Yes I can confirm that this PR fixes the bug I reported |
Matthew Parkinson (mjp41)
approved these changes
Oct 1, 2026
Matthew Parkinson (mjp41)
marked this pull request as ready for review
October 1, 2026 06:33
Matthew Parkinson (mjp41)
deleted the
copilot/issue-885-fix-teardown-order
branch
October 1, 2026 06:35
Member
|
Neil Monday (@NeilMonday) Sorry, I should have got you to comment before I merged. Can you confirm your use case is not affected. Could you confirm it is okay please. I would like to make a release with this fix. |
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.
Since #773,
~VirtualVectorreleases all pagemap reservations during the C runtime's teardown. For an exe that is too early: fiber-local storage (FLS) callbacks run afterwards, insideExitProcess(LdrShutdownProcess). That includes Rust's thread-local destructors, which then touch released memory and crash. This PR keeps the #773 behaviour (release when a DLL is unloaded while the process keeps running) and stops releasing at process exit, in a single build with no config flag.Exit order on Windows
RtlDllShutdownInProgress()in~VirtualVectorexit()→atexitlistExitProcessFALSEDLL_PROCESS_DETACHTRUEFreeLibraryDLL_PROCESS_DETACHFALSEThe flag alone can't tell an exe apart from a DLL being unloaded by
FreeLibrary, so the module type is checked as well.Changes
pal_windows.hPALWindows::current_module_is_dll()readsIMAGE_FILE_DLLfrom the__ImageBaseheaders.RtlDllShutdownInProgressis declared directly. MSDN documents it as exported by ntdll and not declared in SDK headers, so the caller must declare it.~VirtualVectorreturns early unless the module is a DLL and shutdown is not in progress:init_segandreservationsare updated to describe this.ntdll#pragma comment(lib, "ntdll.lib").target_link_libraries(snmalloc INTERFACE ntdll).snmalloc-sys/build.rs:rustc-link-lib=ntdllfor both the MSVC and windows-gnu targets.TESTLIB_ONLY_TESTS)func/fls_teardownreproduces the Rust pattern without Rust. It registers an FLS callback in an exe that touches and frees snmalloc memory at exit.func/dll_teardownuses two helper DLLs:allocallocates and is unloaded withFreeLibrary; the test checks that the memory was released.touchchecks duringDLL_PROCESS_DETACHat process exit that the allocation is still readable. If not, it callsTerminateProcess(1), because the loader swallows faults raised inDllMain.For reviewers
ntdll.libexportsRtlDllShutdownInProgressfor both x86 and x64.thread_localdestructors loaded. TheFreeLibraryrelease check indll_teardownis skipped when the DLL is still loaded; confirm that under MSVC it actually runs.