Skip to content

Dispose MultiSigScanner file handles instead of closing them raw - #177

Merged
marzent merged 1 commit into
marzent:mainfrom
aetherfl0w:u/fix/hook-after-fetch
Nov 26, 2026
Merged

marzent merged 1 commit into
marzent:mainfrom
aetherfl0w:u/fix/hook-after-fetch

Conversation

@hareeen

@hareeen hareeen commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Start the ZoneDownHookManager task after GetFfxivPlugin returns instead of at the top of the plugin constructor.

Context:

Installing the hook suspends every other thread, and under Wine a thread suspended mid file I/O resumes with an invalid handle. With a known game version the hook lands before any file I/O starts, but the dynamic fallback sig-scans for a couple hundred milliseconds first and pushes the install right into the extraction window.

Reproduced on a Korean client under Wine.

@marzent

marzent commented Nov 21, 2026

Copy link
Copy Markdown
Owner

This seems like a wine bug honestly... (also why would a suspend magically cause an invalid handle?)

SetupCopy closed the file and mapping handles with CloseHandle on DangerousGetHandle, which leaves both SafeHandles undisposed.
When the GC later finalizes them, they close the same handle values again.
By then those values can belong to another open file, such as FFXIV_ACT_Plugin.dll while FetchDependencies patches it, which then fails with ObjectDisposedException: Cannot access a closed file.
Dispose the SafeHandles with using instead.
@hareeen
hareeen force-pushed the u/fix/hook-after-fetch branch from cae0f9c to a08d962 Compare November 23, 2026 15:08
@hareeen

hareeen commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Oh sorry, I dug into this more (with some help from Claude) and my original explanation was wrong.

MultiSigScanner.SetupCopy closes its handles with a raw CloseHandle(handle.DangerousGetHandle()), so when the GC finalizes the SafeHandles later, they close the same values again. By then one of them has been reused for FFXIV_ACT_Plugin.dll, so Cecil loses the file while it's parsing the plugin for patching.

Wineserver trace (WINEDEBUG=+server):

0404: create_file() = 0 { handle=0acc }        // scanner opens ffxiv_dx11.exe
0404: create_mapping() = 0 { handle=0b48 }
0404: close_handle( handle=0acc )              // raw CloseHandle
0404: close_handle( handle=0b48 )
...
042c: create_file() = 0 { handle=0acc }        // fetch thread reuses 0acc for FFXIV_ACT_Plugin.dll
0228: close_handle( handle=0b48 )              // finalizer closes them again
0228: close_handle( handle=0acc )
042c: get_handle_fd() = INVALID_HANDLE

Fixed it by using the handles instead (now it loads fine on a fresh install).

@hareeen hareeen changed the title Start zone-down hook after fetching the ACT plugin Dispose MultiSigScanner file handles instead of closing them raw Nov 23, 2026

@marzent marzent left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this does make sense now, thanks for investigating this.

@marzent
marzent merged commit 3cd0b68 into marzent:main Nov 26, 2026
1 check passed
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.

2 participants