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

Only enable IOCP notifications in Process#wait, not .new #14995

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions spec/std/process_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ describe Process do
end
end
end

it "doesn't break if process is collected before completion" do
200.times { Process.new(*exit_code_command(0)) }
10.times { GC.collect }
end
end

describe "#wait" do
Expand Down
22 changes: 11 additions & 11 deletions src/crystal/system/win32/process.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,7 @@ struct Crystal::System::Process

@job_object = LibC.CreateJobObjectW(nil, nil)

# enable IOCP notifications
config_job_object(
LibC::JOBOBJECTINFOCLASS::AssociateCompletionPortInformation,
LibC::JOBOBJECT_ASSOCIATE_COMPLETION_PORT.new(
completionKey: @completion_key.as(Void*),
completionPort: Crystal::EventLoop.current.iocp,
),
)

# but not for any child processes
# disable IOCP notifications for any child processes
config_job_object(
LibC::JOBOBJECTINFOCLASS::ExtendedLimitInformation,
LibC::JOBOBJECT_EXTENDED_LIMIT_INFORMATION.new(
Expand Down Expand Up @@ -71,6 +62,16 @@ struct Crystal::System::Process
end

def wait
# enable IOCP notifications
@completion_key.fiber = ::Fiber.current
config_job_object(
LibC::JOBOBJECTINFOCLASS::AssociateCompletionPortInformation,
LibC::JOBOBJECT_ASSOCIATE_COMPLETION_PORT.new(
completionKey: @completion_key.as(Void*),
completionPort: Crystal::EventLoop.current.iocp,
),
)

if LibC.GetExitCodeProcess(@process_handle, out exit_code) == 0
raise RuntimeError.from_winerror("GetExitCodeProcess")
end
Expand All @@ -80,7 +81,6 @@ struct Crystal::System::Process
# TODO: message delivery is "not guaranteed"; does it ever happen? Are we
# stuck forever in that case?
# (https://learn.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject_associate_completion_port)
@completion_key.fiber = ::Fiber.current
::Fiber.suspend

# If the IOCP notification is delivered before the process fully exits,
Expand Down
Loading