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

Enqueue senders in Channel#close #5880

Merged
merged 1 commit into from
Mar 30, 2018
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
23 changes: 23 additions & 0 deletions spec/std/channel_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,29 @@ describe Channel::Unbuffered do
spawn { ch.send 123 }
ch.receive?.should eq(123)
end

it "wakes up the sender fiber when channel is closed" do
ch = Channel::Unbuffered(Nil).new
sender_closed = false
spawn do
ch.send nil
ch.send nil
rescue Channel::ClosedError
sender_closed = true
end
receiver_closed = false
spawn do
Fiber.yield
ch.receive
rescue Channel::ClosedError
receiver_closed = true
end
Fiber.yield
ch.close
Fiber.yield
sender_closed.should be_true
receiver_closed.should be_true
end
end

describe Channel::Buffered do
Expand Down
11 changes: 11 additions & 0 deletions src/concurrent/channel.cr
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ abstract class Channel(T)

def close
@closed = true
Scheduler.enqueue @senders
@senders.clear
Scheduler.enqueue @receivers
@receivers.clear
nil
Expand Down Expand Up @@ -259,6 +261,7 @@ class Channel::Unbuffered(T) < Channel(T)
@value.tap do
@has_value = false
Scheduler.enqueue @sender.not_nil!
@sender = nil
end
end

Expand All @@ -269,4 +272,12 @@ class Channel::Unbuffered(T) < Channel(T)
def full?
@has_value || @receivers.empty?
end

def close
super
if sender = @sender
Copy link
Contributor

Choose a reason for hiding this comment

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

You should set @sender = nil here to prevent any possibility of @sender being enqueued twice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks!

Scheduler.enqueue sender
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure but @sender may have been be already enqueued, see https://github.com/carlhoerberg/crystal/blob/c901b8096fff57893c46555401a395ca1c0a1aef/src/concurrent/channel.cr#L263. Maybe @sender should be set to nil in #receive_impl?

@value.tap do
  @has_value = false
  Scheduler.enqueue @sender.not_nil!
  @sender = nil
end

@sender = nil
end
end
end