Skip to content
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
10 changes: 5 additions & 5 deletions lib/concurrent-ruby/concurrent/executor/safe_task_executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ def initialize(task, opts = {})

# @return [Array]
def execute(*args)
synchronize do
success = false
value = reason = nil
success = true
value = reason = nil

synchronize do
begin
value = @task.call(*args)
success = true
rescue @exception_class => ex
reason = ex
success = false
end

[success, value, reason]
end

[success, value, reason]
Copy link
Collaborator

@eregon eregon Mar 16, 2022

Choose a reason for hiding this comment

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

I don't understand this change, it should never make a difference as far as I can tell.
All possibilities are:

  • @task.call does not raise an exception, so success = true
  • @task.call raises something caught by rescue @exception_class, so success = false
  • @task.call raises something NOT caught by rescue @exception_class (e.g., like a break or return or exception higher in the hirearchy), then there should be no way we would ever reach the end of this method, or even the end of the synchronize block (and so success is never used).

So the only case I could see this would make a difference is a bug in CRuby where it break or return to the semantically incorrect place, such as after that synchronize call (which should only be possible with a break syntactically inside the synchronize block, and there isn't any).

That said, this change is not hurting either, so I think it's fine to keep the change, but we should have a better understanding why it's needed, and a clearer test (see #931 (comment)).

end
end
end
29 changes: 29 additions & 0 deletions spec/concurrent/executor/safe_task_executor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,35 @@ module Concurrent
}.to raise_error(Exception)
end
end

context 'local jump error' do
def execute
Thread.new do
executor = SafeTaskExecutor.new(-> { yield 42 })
@result = executor.execute
end.join
end

subject do
to_enum(:execute).first
@result
end

it 'should return success' do
success, _value, _reason = subject
expect(success).to be_truthy
end

it 'should return a nil value' do
_success, value, _reason = subject
expect(value).to be_nil
end

it 'should return a nil reason' do
_success, _value, reason = subject
expect(reason).to be_nil
end
end
end
end
end