"Puts" errors and breaks program when used in a Fiber on Windows #7955
Labels
kind:bug
A bug in the code. Does not apply to documentation, specs, etc.
platform:windows
Windows support based on the MSVC toolchain
Problem
When
puts
is used in a context switch on Windows it causes an error which is subtle since it prevents anything from being written to the console at all so there are no visible errors given and the code simply seems to break.Solving this presumably helps #6955 move along and is the real error causing #7947.
Details
When
puts
is called after any context switch on Windows, like a fiber, it errors with the messageFile not open for writing
. This relevant part of the source code raising this error is found here. What makes this troublesome is that the error message itself is not being printed either. This might be caused by some kind of lock contention where a lock onstdout
is not released as expected.However if you replace:
With:
It works.
To see the actual error you'll have to disassemble the code using a disassembler for Windows and step through the code instruction by instruction. After the message is supposed to be printed you'll see one of the CPU registers pointing at a memory location containing the string "File not open for writing", but the error message never actually gets printed.
How to reproduce
Since Fibers for Windows is not implemented yet (coincidentally since the implementation didn't seem to work because of this very error), I've written an absolute minimum amount of code to reproduce this error on the current compiler (v0.29):
I would suggest you use Windows and WSL to test this
puts_error.cr
if you want to copy paste the rest of the commands belowcrystal build -D gc_none --cross-compile --target x86_64-pc-windows-msvc puts_error.cr
.o
file to the folder where you havepcre.lib
andgc.lib
in the root folder. Openx86_64 Cross Tools Command Promt for vs 2019
and write this command:cl "puts_error.o" "/Feputs_error" pcre.lib gc.lib advapi32.lib libcmt.lib legacy_stdio_definitions.lib
puts_error.exe
. The program seems to just not work.puts
withSTDOUT
like shown at the bottom an see the message printed.If you replace
puts "Hello from new stack
with:It works as expected on both Linux and Windows.
The text was updated successfully, but these errors were encountered: