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

Hookable thread abort (needed for inline RQL unlocking for some critical places of our business logic) #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 14 additions & 7 deletions lib/rack/timeout/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ module Rack
class Timeout
include Rack::Timeout::MonotonicTime # gets us the #fsecs method

# NOTE: umbrellio-patch (START): an ability to set up custom hook for thread abort exception
@__custom_config = { on_thread_abort_hooks: Set.new }
class << self
attr_reader :__custom_config
def add_on_thread_abort_hook(&block)
__custom_config[:on_thread_abort_hooks] << block
end
end
# NOTE: umbrellio-patch (END): an ability to set up custom hook for thread abort exception

module ExceptionWithEnv # shared by the following exceptions, allows them to receive the current env
attr :env
def initialize(env)
Expand Down Expand Up @@ -74,13 +84,6 @@ def initialize(app, service_timeout:nil, wait_timeout:nil, wait_overtime:nil, se
@service_past_wait = service_past_wait == "not_specified" ? ENV.fetch("RACK_TIMEOUT_SERVICE_PAST_WAIT", false).to_s != "false" : service_past_wait

if @term_on_timeout && !::Process.respond_to?(:fork)
raise(NotImplementedError, <<-MSG)
The platform running your application does not support forking (i.e. Windows, JVM, etc).

To avoid this error, either specify RACK_TIMEOUT_TERM_ON_TIMEOUT=0 or
leave it as default (which will have the same result).

MSG
end
@app = app
end
Expand Down Expand Up @@ -147,6 +150,10 @@ def call(env)
end
end

# NOTE: umbrellio-patch (START): an ability to set up custom hook for thread abort exception
::Rack::Timeout.__custom_config[:on_thread_abort_hooks].each { |hook| hook.call(app_thread, @app, env) }
# NOTE: umbrellio-patch (END): an ability to set up custom hook for thread abort exception

app_thread.raise(RequestTimeoutException.new(env), message)
end

Expand Down