Skip to content

Commit 8956b90

Browse files
committed
auto_termination related cleanups
- disable_at_exit_handlers! has to be kept as noop not to break current usages - cleanup auto_terminate handling - remove ns_auto_terminate attribute - deprecate auto_terminate=
1 parent 4a16092 commit 8956b90

14 files changed

+44
-29
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## Current
22

3+
concurrent-ruby:
4+
5+
* Concurrent.disable_at_exit_handlers! is not longer needed and was deprecated.
6+
* AbstractExecutorService#auto_terminate= was deprecated and has no effect.
7+
Set :auto_terminate option instead when executor is initialized.
8+
39
## Release v1.1.6.pre1, edge v0.6.0.pre1 (26 Jan 2020)
410

511
concurrent-ruby:

lib/concurrent-ruby-edge/concurrent/channel.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Channel
1515
include Enumerable
1616

1717
# NOTE: Move to global IO pool once stable
18-
GOROUTINES = Concurrent::CachedThreadPool.new(auto_terminate: true)
18+
GOROUTINES = Concurrent::CachedThreadPool.new
1919
private_constant :GOROUTINES
2020

2121
BUFFER_TYPES = {

lib/concurrent-ruby/concurrent/configuration.rb

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
require 'concurrent/errors'
44
require 'concurrent/atomic/atomic_reference'
55
require 'concurrent/concern/logging'
6+
require 'concurrent/concern/deprecation'
67
require 'concurrent/executor/immediate_executor'
78
require 'concurrent/executor/cached_thread_pool'
89
require 'concurrent/utility/processor_counter'
910

1011
module Concurrent
1112
extend Concern::Logging
13+
extend Concern::Deprecation
1214

1315
autoload :Options, 'concurrent/options'
1416
autoload :TimerSet, 'concurrent/executor/timer_set'
@@ -96,21 +98,40 @@ def self.global_logger=(value)
9698
end
9799

98100
# @!visibility private
99-
GLOBAL_FAST_EXECUTOR = Delay.new { Concurrent.new_fast_executor(auto_terminate: true) }
101+
GLOBAL_FAST_EXECUTOR = Delay.new { Concurrent.new_fast_executor }
100102
private_constant :GLOBAL_FAST_EXECUTOR
101103

102104
# @!visibility private
103-
GLOBAL_IO_EXECUTOR = Delay.new { Concurrent.new_io_executor(auto_terminate: true) }
105+
GLOBAL_IO_EXECUTOR = Delay.new { Concurrent.new_io_executor }
104106
private_constant :GLOBAL_IO_EXECUTOR
105107

106108
# @!visibility private
107-
GLOBAL_TIMER_SET = Delay.new { TimerSet.new(auto_terminate: true) }
109+
GLOBAL_TIMER_SET = Delay.new { TimerSet.new }
108110
private_constant :GLOBAL_TIMER_SET
109111

110112
# @!visibility private
111113
GLOBAL_IMMEDIATE_EXECUTOR = ImmediateExecutor.new
112114
private_constant :GLOBAL_IMMEDIATE_EXECUTOR
113115

116+
# Disables AtExit handlers including pool auto-termination handlers.
117+
# When disabled it will be the application programmer's responsibility
118+
# to ensure that the handlers are shutdown properly prior to application
119+
# exit by calling `AtExit.run` method.
120+
#
121+
# @note this option should be needed only because of `at_exit` ordering
122+
# issues which may arise when running some of the testing frameworks.
123+
# E.g. Minitest's test-suite runs itself in `at_exit` callback which
124+
# executes after the pools are already terminated. Then auto termination
125+
# needs to be disabled and called manually after test-suite ends.
126+
# @note This method should *never* be called
127+
# from within a gem. It should *only* be used from within the main
128+
# application and even then it should be used only when necessary.
129+
# @deprecated Has no effect since it is not longer needed, see https://github.com/ruby-concurrency/concurrent-ruby/pull/841.
130+
#
131+
def self.disable_at_exit_handlers!
132+
deprecated "Method #disable_at_exit_handlers! has no effect since it is not longer needed, see https://github.com/ruby-concurrency/concurrent-ruby/pull/841."
133+
end
134+
114135
# Global thread pool optimized for short, fast *operations*.
115136
#
116137
# @return [ThreadPoolExecutor] the thread pool

lib/concurrent-ruby/concurrent/executor/abstract_executor_service.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'concurrent/errors'
2+
require 'concurrent/concern/deprecation'
23
require 'concurrent/executor/executor_service'
34
require 'concurrent/synchronization'
45

@@ -8,6 +9,7 @@ module Concurrent
89
# @!visibility private
910
class AbstractExecutorService < Synchronization::LockableObject
1011
include ExecutorService
12+
include Concern::Deprecation
1113

1214
# The set of possible fallback policies that may be set at thread pool creation.
1315
FALLBACK_POLICIES = [:abort, :discard, :caller_runs].freeze
@@ -17,14 +19,14 @@ class AbstractExecutorService < Synchronization::LockableObject
1719

1820
attr_reader :name
1921

20-
attr_accessor :ns_auto_terminate
21-
2222
# Create a new thread pool.
2323
def initialize(opts = {}, &block)
2424
super(&nil)
2525
synchronize do
26-
ns_initialize(opts, &block)
26+
# TODO (pitr-ch 02-Feb-2020): check all documentation
27+
@auto_terminate = opts.fetch(:auto_terminate, true)
2728
@name = opts.fetch(:name) if opts.key?(:name)
29+
ns_initialize(opts, &block)
2830
end
2931
end
3032

@@ -64,12 +66,12 @@ def shutdown?
6466

6567
# @!macro executor_service_method_auto_terminate_question
6668
def auto_terminate?
67-
synchronize { ns_auto_terminate? }
69+
synchronize { @auto_terminate }
6870
end
6971

7072
# @!macro executor_service_method_auto_terminate_setter
7173
def auto_terminate=(value)
72-
synchronize { self.ns_auto_terminate = value }
74+
deprecated "Method #auto_terminate= has no effect. Set :auto_terminate option when executor is initialized."
7375
end
7476

7577
private
@@ -120,7 +122,7 @@ def ns_kill_execution
120122
end
121123

122124
def ns_auto_terminate?
123-
!!ns_auto_terminate
125+
@auto_terminate
124126
end
125127

126128
end

lib/concurrent-ruby/concurrent/executor/cached_thread_pool.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,9 @@ def initialize(opts = {})
5151
def ns_initialize(opts)
5252
super(opts)
5353
if Concurrent.on_jruby?
54-
self.auto_terminate = opts.fetch(:auto_terminate, true)
5554
@max_queue = 0
5655
@executor = java.util.concurrent.Executors.newCachedThreadPool(
57-
DaemonThreadFactory.new(self.auto_terminate?))
56+
DaemonThreadFactory.new(ns_auto_terminate?))
5857
@executor.setRejectedExecutionHandler(FALLBACK_POLICY_CLASSES[@fallback_policy].new)
5958
@executor.setKeepAliveTime(opts.fetch(:idletime, DEFAULT_THREAD_IDLETIMEOUT), java.util.concurrent.TimeUnit::SECONDS)
6059
end

lib/concurrent-ruby/concurrent/executor/executor_service.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ module Concurrent
111111

112112
# @!macro executor_service_method_auto_terminate_setter
113113
#
114-
# Set the auto-terminate behavior for this executor.
115114
#
115+
# Set the auto-terminate behavior for this executor.
116+
# @deprecated Has no effect
116117
# @param [Boolean] value The new auto-terminate value to set for this executor.
117-
#
118118
# @return [Boolean] `true` when auto-termination is enabled else `false`.
119119

120120
###################################################################

lib/concurrent-ruby/concurrent/executor/java_executor_service.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,13 @@ def wait_for_termination(timeout = nil)
3838

3939
def shutdown
4040
synchronize do
41-
self.ns_auto_terminate = false
4241
@executor.shutdown
4342
nil
4443
end
4544
end
4645

4746
def kill
4847
synchronize do
49-
self.ns_auto_terminate = false
5048
@executor.shutdownNow
5149
nil
5250
end

lib/concurrent-ruby/concurrent/executor/java_single_thread_executor.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ def initialize(opts = {})
1717
end
1818

1919
private
20-
20+
2121
def ns_initialize(opts)
2222
@executor = java.util.concurrent.Executors.newSingleThreadExecutor
2323
@fallback_policy = opts.fetch(:fallback_policy, :discard)
2424
raise ArgumentError.new("#{@fallback_policy} is not a valid fallback policy") unless FALLBACK_POLICY_CLASSES.keys.include?(@fallback_policy)
25-
self.auto_terminate = opts.fetch(:auto_terminate, true)
2625
end
2726
end
2827
end

lib/concurrent-ruby/concurrent/executor/java_thread_pool_executor.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,13 @@ def ns_initialize(opts)
108108
queue = java.util.concurrent.LinkedBlockingQueue.new(@max_queue)
109109
end
110110

111-
self.auto_terminate = opts.fetch(:auto_terminate, true)
112-
113111
@executor = java.util.concurrent.ThreadPoolExecutor.new(
114112
min_length,
115113
max_length,
116114
idletime,
117115
java.util.concurrent.TimeUnit::SECONDS,
118116
queue,
119-
DaemonThreadFactory.new(self.auto_terminate?),
117+
DaemonThreadFactory.new(ns_auto_terminate?),
120118
FALLBACK_POLICY_CLASSES[@fallback_policy].new)
121119

122120
end

lib/concurrent-ruby/concurrent/executor/ruby_executor_service.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ def post(*args, &task)
2727
def shutdown
2828
synchronize do
2929
break unless running?
30-
self.ns_auto_terminate = false
3130
stop_event.set
3231
ns_shutdown_execution
3332
end
@@ -37,7 +36,6 @@ def shutdown
3736
def kill
3837
synchronize do
3938
break if shutdown?
40-
self.ns_auto_terminate = false
4139
stop_event.set
4240
ns_kill_execution
4341
stopped_event.set

0 commit comments

Comments
 (0)