ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:91703] [Ruby trunk Bug#15644] ThreadsWait problems with Thread#report_on_exception
       [not found] <redmine.issue-15644.20190306132045@ruby-lang.org>
@ 2019-03-06 13:20 ` kimmo.lehto
  2019-10-17 18:49 ` [ruby-core:95400] [Ruby master " merch-redmine
  1 sibling, 0 replies; 2+ messages in thread
From: kimmo.lehto @ 2019-03-06 13:20 UTC (permalink / raw)
  To: ruby-core

Issue #15644 has been reported by kke (Kimmo Lehto).

----------------------------------------
Bug #15644: ThreadsWait problems with Thread#report_on_exception
https://bugs.ruby-lang.org/issues/15644

* Author: kke (Kimmo Lehto)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.6.1p33 (2019-01-30 revision 66950) [x86_64-darwin18]
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
ThreadsWait spawns a new thread for waiting on a thread:

```
  # thwait.rb:87
  def join_nowait(*threads)
    threads.flatten!
    @threads.concat threads
    for th in threads
      Thread.start(th) do |t|
        begin
          t.join
        ensure
          @wait_queue.push t
        end
      end
    end
  end
```

As `thread.join` raises the exception from the thread, it will trigger a thread exception report.

If the thread was using `report_on_exception = false`, the wait thread will report the exception anyway.

If the thread was using `report_on_exception = true`, both threads will report the exception.

I think this could be fixed by always setting `report_on_exception = false` for the wait thread.

# Example 1

```
require 'thwait'

thread = Thread.new do
  Thread.current.report_on_exception = false
  raise "Foo"
end

ThreadsWait.all_waits(thread) do |terminated_thread|
  puts "Thread #{terminated_thread} terminated"
end
```

## Expected result

No exception reports

## Actual result

The wait thread will report the exception


# Example 2

```
require 'thwait'

thread = Thread.new do
  raise "Foo"
end

ThreadsWait.all_waits(thread) do |terminated_thread|
  puts "Thread #{terminated_thread} terminated"
end
```

## Expected result

One exception report

## Actual result

Two exception reports




-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [ruby-core:95400] [Ruby master Bug#15644] ThreadsWait problems with Thread#report_on_exception
       [not found] <redmine.issue-15644.20190306132045@ruby-lang.org>
  2019-03-06 13:20 ` [ruby-core:91703] [Ruby trunk Bug#15644] ThreadsWait problems with Thread#report_on_exception kimmo.lehto
@ 2019-10-17 18:49 ` merch-redmine
  1 sibling, 0 replies; 2+ messages in thread
From: merch-redmine @ 2019-10-17 18:49 UTC (permalink / raw)
  To: ruby-core

Issue #15644 has been updated by jeremyevans0 (Jeremy Evans).

Assignee set to keiju (Keiju Ishitsuka)
Status changed from Open to Assigned

I agree this is a bug that should be fixed.  I have submitted a pull request to fix it: https://github.com/ruby/thwait/pull/1

----------------------------------------
Bug #15644: ThreadsWait problems with Thread#report_on_exception
https://bugs.ruby-lang.org/issues/15644#change-82123

* Author: kke (Kimmo Lehto)
* Status: Assigned
* Priority: Normal
* Assignee: keiju (Keiju Ishitsuka)
* Target version: 
* ruby -v: ruby 2.6.1p33 (2019-01-30 revision 66950) [x86_64-darwin18]
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
Using ThreadsWait with Thread.report_on_exception is confusing.

ThreadsWait spawns a new thread for waiting on a thread:

```
  # thwait.rb:87
  def join_nowait(*threads)
    threads.flatten!
    @threads.concat threads
    for th in threads
      Thread.start(th) do |t|
        begin
          t.join
        ensure
          @wait_queue.push t
        end
      end
    end
  end
```

The `t.join` in `ThreadsWait` will re-raise the exceptions that happen in the thread being waited on.

If the thread being waited on was using `report_on_exception = false`, the wait thread will report the exception and the `report_on_exception = false` effectively did nothing, as you still got an exception report. 

If the thread was using `report_on_exception = true`, both threads will report the exception, so you get it twice.

I think this could be fixed by always setting `report_on_exception = false` for the wait thread.

# Example 1

```
require 'thwait'

thread = Thread.new do
  Thread.current.report_on_exception = false
  sleep 1
  raise "Foo"
end

ThreadsWait.all_waits(thread) do |terminated_thread|
  puts "Thread #{terminated_thread} terminated"
end
```

## Expected result

No exception reports

## Actual result

The wait thread will report the exception:

```
#<Thread:0x00007fa57d092d68@/Users/user/.rbenv/versions/2.6.1/lib/ruby/2.6.0/thwait.rb:91 run> terminated with exception (report_on_exception is true):
```


# Example 2

```
require 'thwait'

thread = Thread.new do
  sleep 1
  raise "Foo"
end

ThreadsWait.all_waits(thread) do |terminated_thread|
  puts "Thread #{terminated_thread} terminated"
end
```

## Expected result

One exception report

## Actual result

Two exception reports:

```
#<Thread:0x00007f9f83053320@test.rb:3 run> terminated with exception (report_on_exception is true):
#<Thread:0x00007f9f830530a0@/Users/user/.rbenv/versions/2.6.1/lib/ruby/2.6.0/thwait.rb:91 run> terminated with exception (report_on_exception is true):
Traceback (most recent call last):
```





-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-10-17 18:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-15644.20190306132045@ruby-lang.org>
2019-03-06 13:20 ` [ruby-core:91703] [Ruby trunk Bug#15644] ThreadsWait problems with Thread#report_on_exception kimmo.lehto
2019-10-17 18:49 ` [ruby-core:95400] [Ruby master " merch-redmine

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).