Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

win32: GetLastError and threads #4979

vicuna opened this Issue Feb 16, 2010 · 2 comments


None yet
2 participants
Copy link

vicuna commented Feb 16, 2010

Original bug ID: 4979
Reporter: gerd
Assigned to: @xavierleroy
Status: closed (set by @xavierleroy on 2011-05-29T10:19:34Z)
Resolution: fixed
Priority: normal
Severity: minor
Version: 3.11.1
Fixed in version: 3.12.0+dev
Category: ~DO NOT USE (was: OCaml general)

Bug description

Just got an error Sys_error("Mutex.lock: error code 0") in a win32 program using threads heavily. I wondered how that was possible. Apparently, the true reason of the error is hidden, and the error code 0 is a lie. (It's usually an invalid descriptor.)

So I dug a bit deeper into the ocaml runtime, and found this (systhreads/win32.c):

CAMLprim value caml_mutex_lock(value mut)
int retcode;
Begin_root(mut) /* prevent deallocation of mutex */
retcode = WaitForSingleObject(Mutex_val(mut), INFINITE);
if (retcode == WAIT_FAILED) caml_wthread_error("Mutex.lock");
return Val_unit;

In caml_wthread_error, the win32 function GetLastError is used to obtain the error code. However, this code was already overwritten in leave_block_section (there is a WaitForSingleObject right at the beginning to get the master lock).

Suggested fix: Get the error code before leave_blocking_section, and pass it to caml_wthread_error.


This comment has been minimized.

Copy link
Collaborator Author

vicuna commented Feb 17, 2010

Comment author: @damiendoligez

There is a second bug in the above code: the heap is accessed (by Mutex_val) from within the blocking section.


This comment has been minimized.

Copy link
Collaborator Author

vicuna commented Apr 27, 2010

Comment author: @xavierleroy

Fixed as part of a general refactoring of systhreads implementation. Will go in 3.12.0.

@vicuna vicuna closed this May 29, 2011

@vicuna vicuna added the bug label Mar 20, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.