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

Reserve stack space on non-main threads for crash recovery on Windows #14187

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/crystal/system/win32/thread.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,24 @@ module Crystal::System::Thread
@system_handle = GC.beginthreadex(
security: Pointer(Void).null,
stack_size: LibC::UInt.zero,
start_address: ->(data : Void*) { data.as(::Thread).start; LibC::UInt.zero },
start_address: ->Thread.thread_proc(Void*),
arglist: self.as(Void*),
initflag: LibC::UInt.zero,
thrdaddr: Pointer(LibC::UInt).null,
)
end

def self.thread_proc(data : Void*) : LibC::UInt
# ensure that even in the case of stack overflow there is enough reserved
# stack space for recovery (for the main thread this is done in
# `Exception::CallStack.setup_crash_handler`)
stack_size = Crystal::System::Fiber::RESERVED_STACK_SIZE
LibC.SetThreadStackGuarantee(pointerof(stack_size))

data.as(::Thread).start
LibC::UInt.zero
end

def self.current_handle : Handle
# `GetCurrentThread` returns a _constant_ and is only meaningful as an
# argument to Win32 APIs; to uniquely identify it we must duplicate the handle
Expand Down
3 changes: 2 additions & 1 deletion src/exception/call_stack/stackwalk.cr
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ struct Exception::CallStack
end)

# ensure that even in the case of stack overflow there is enough reserved
# stack space for recovery
# stack space for recovery (for other threads this is done in
# `Crystal::System::Thread.thread_proc`)
stack_size = Crystal::System::Fiber::RESERVED_STACK_SIZE
LibC.SetThreadStackGuarantee(pointerof(stack_size))
end
Expand Down