Skip to content

Simplify Crystal::System::Fiber::RESERVED_STACK_SIZE initializer on Windows#15820

Merged
straight-shoota merged 2 commits intocrystal-lang:masterfrom
HertzDevil:bug/windows-stack-size-const
May 26, 2025
Merged

Simplify Crystal::System::Fiber::RESERVED_STACK_SIZE initializer on Windows#15820
straight-shoota merged 2 commits intocrystal-lang:masterfrom
HertzDevil:bug/windows-stack-size-const

Conversation

@HertzDevil
Copy link
Contributor

@HertzDevil HertzDevil commented May 23, 2025

LibC::DWORD.new(0x10000) is a complex initializer, which on very rare occasions could completely halt the program:

#0  0x00007ffb15544f44 in ntdll!ZwRemoveIoCompletionEx () from C:\WINDOWS\SYSTEM32\ntdll.dll
#1  0x00007ffb12e98cef in KERNELBASE!GetQueuedCompletionStatusEx () from C:\WINDOWS\System32\KernelBase.dll      
#2  0x00007ff6c4ecc77c in run () at C:\Users\nicet\crystal\crystal\src\crystal\system\win32/iocp.cr:107
#3  0x00007ff6c4ec9175 in reschedule () at C:\Users\nicet\crystal\crystal\src\crystal/scheduler.cr:144
#4  0x00007ff6c4ec90da in reschedule () at C:\Users\nicet\crystal\crystal\src\crystal/scheduler.cr:62
#5  0x00007ff6c4ebfe89 in suspend () at C:\Users\nicet\crystal\crystal\src/fiber.cr:351
#6  0x00007ff6c4f0d046 in wait_initializer () at C:\Users\nicet\crystal\crystal\src\crystal/once.cr:81
#7  0x00007ff6c4ed588d in once () at C:\Users\nicet\crystal\crystal\src\crystal/once.cr:51
#8  0x00007ff6c4e41879 in __crystal_once () at C:\Users\nicet\crystal\crystal\src\crystal/once.cr:124
#9  0x00007ff6c4e4168b in __crystal_main ()
    at C:\Users\nicet\crystal\crystal\src\crystal\system\win32/fiber.cr:8
#10 0x00007ff6c4ed5b29 in main_user_code () at C:\Users\nicet\crystal\crystal\src\crystal/main.cr:129
#11 0x00007ff6c4ed5a71 in main () at C:\Users\nicet\crystal\crystal\src\crystal/main.cr:115
#12 0x00007ff6c4e44f82 in main () at C:\Users\nicet\crystal\crystal\src\crystal/main.cr:141

Maybe it was interfering with the event loop before fiber stacks could be checked out? This was especially observable on the recent ARM64 MSYS2 CI, but if you run a Hello World program in a shell loop the same also happens in a matter of seconds.

@HertzDevil HertzDevil added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:concurrency platform:windows Windows support based on the MSVC toolchain / Win32 API labels May 23, 2025
@HertzDevil HertzDevil moved this to Review in Windows Support May 23, 2025
Copy link
Collaborator

@ysbaddaden ysbaddaden left a comment

Choose a reason for hiding this comment

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

What about LibC::DWORD.new!(0x10000) to skip the math overflow check?

@HertzDevil
Copy link
Contributor Author

The overflow check is not the issue here, that is still a complex initializer that requires __crystal_once and is not inlined by the compiler. Ideally we want #13443 for this

@ysbaddaden
Copy link
Collaborator

Oh right, it's still a constant 🤦

@straight-shoota straight-shoota added this to the 1.17.0 milestone May 23, 2025
@straight-shoota straight-shoota merged commit 9e68a1d into crystal-lang:master May 26, 2025
41 of 42 checks passed
@github-project-automation github-project-automation bot moved this from Review to Done in Windows Support May 26, 2025
@HertzDevil HertzDevil deleted the bug/windows-stack-size-const branch May 26, 2025 09:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind:bug A bug in the code. Does not apply to documentation, specs, etc. platform:windows Windows support based on the MSVC toolchain / Win32 API topic:stdlib:concurrency

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants