Skip to content

fix(stdune): preserve Windows flock handles and errors#15528

Open
rgrinberg wants to merge 1 commit into
ocaml:mainfrom
rgrinberg:push-wmzrqllvnosy
Open

fix(stdune): preserve Windows flock handles and errors#15528
rgrinberg wants to merge 1 commit into
ocaml:mainfrom
rgrinberg:push-wmzrqllvnosy

Conversation

@rgrinberg

Copy link
Copy Markdown
Member

Handle_val dereferences an OCaml custom block and must not be evaluated
after releasing the runtime. Reacquiring the runtime can also overwrite
the thread-local Windows last-error value from LockFileEx or
UnlockFileEx.

Extract each handle while holding the runtime and save GetLastError
immediately after a failed system call.

Handle_val dereferences an OCaml custom block and must not be evaluated
after releasing the runtime. Reacquiring the runtime can also overwrite
the thread-local Windows last-error value from LockFileEx or
UnlockFileEx.

Extract each handle while holding the runtime and save GetLastError
immediately after a failed system call.

Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
@rgrinberg

rgrinberg commented Jul 18, 2026

Copy link
Copy Markdown
Member Author

@nojb could you double check that this makes sense? It looks plausible to me, but I don't have enough winapi experience to make a judgement.

@nojb nojb left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, but I don't think it is necessary to preserve GetLastError() (it should already be preserved by caml_acquire_runtime_system(), same as errno.

ok = LockFileEx(fd, dwFlags, 0, MAXDWORD, MAXDWORD, &overlapped);
if (!ok) {
error = GetLastError();
}

@nojb nojb Jul 18, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I believe caml_acquire_runtime_system() expressively preserves errno/GetLastError(), so there is no need for this pirouette. See ocaml/ocaml#10220.

@rgrinberg

Copy link
Copy Markdown
Member Author

Good to know, thanks.

@rgrinberg rgrinberg closed this Jul 18, 2026
@nojb

nojb commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator

Good to know, thanks.

Note that the other change, moving FD_val outside the critical section is a valid bug fix I believe (the custom block may move after the runtime lock is released).

@rgrinberg rgrinberg reopened this Jul 18, 2026
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