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

Closed
vicuna opened this issue Feb 16, 2010 · 2 comments
Closed

win32: GetLastError and threads #4979

vicuna opened this issue Feb 16, 2010 · 2 comments
Assignees
Labels

Comments

@vicuna
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 */
enter_blocking_section();
retcode = WaitForSingleObject(Mutex_val(mut), INFINITE);
leave_blocking_section();
End_roots();
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.

@vicuna
Copy link
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.

@vicuna
Copy link
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants