input_char after close_in crashes ocaml (msvc runtime) #4887
Original bug ID: 4887
Problem is specific to msvc runtime.
fd is closed and assigned -1, after that read(-1,...) results in CRT invoking invalid_parameter_handler which takes down the whole process by default.
A simple C program that shows this behavior and a fix is attached. Uncomment line 30 to call _set_invalid_parameter_handler and disable default handler -- _read will return error and program continues as expected.
Looking at MSDN it appears this behaviour was introduced in VS80..
The text was updated successfully, but these errors were encountered:
Comment author: @xavierleroy
Two years after, I'm still confused about what needs to be done: play with _set_invalid_parameter_handler? with _CtrSetReportMode? with both? should we #ifdef the code so that it remains compatible with earlier versions of VS? or not bother? A sample patch that's been tested would be very much appreciated. Thanks.
Comment author: Christoph Bauer
I have a similar problem, when writing to a broken pipe. With the ocaml-mingw
My solution is to install an empty invalid_parameter_handler. Then it is ok again.
BTW, to get a stack trace in Windows I added in config/Makefile to the compiler flags (BYTECCCOMPOPTS, DLLCCCOMPOPTS, NATIVECCCOMPOPTS) a /Zd and to the link flags (MKDLL, MKEXE, MKMAINDLL)
void myInvalidParameterHandler(const wchar_t* expression,
/* empty */
Comment author: @alainfrisch
I've committed your patch to the trunk (r13257), with the following modification: caml_install_invalid_parameter_handler is also called from caml_startup_code in byterun/startup.c (i.e. when the main program is linked with -output-obj in bytecode). It is not very nice to set the global handler if we embed the OCaml code in a larger native application, but this is probably good enough for now (and this is what happens in native code). If someone needs to disable this behavior for a specific application, we can always introduce later some global flags checked by caml_startup_code.
(Also tested with VS 2008, 32-bit.)