Skip to content
This repository has been archived by the owner on Feb 14, 2023. It is now read-only.

Commit

Permalink
kill the main thread of the script if it's unloaded before it finishe…
Browse files Browse the repository at this point in the history
…s loading

such as by openhab/openhab-core#3180
  • Loading branch information
ccutrer committed Nov 28, 2022
1 parent 5b29462 commit fb8fe61
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/openhab/core/script_handling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ def script_unloaded(before: nil, &block)
# @!visibility private
module ScriptHandlingCallbacks
class << self
#
# Has the script completed loading?
#
# @!visibility private
# @return [true, false]
attr_accessor :script_loaded

#
# Return script_loaded_hooks
#
Expand All @@ -78,6 +85,7 @@ def script_unloaded_hooks
@script_unloaded_hooks ||= []
end
end
self.script_loaded = false

#
# Executed when OpenHAB unloads a script file
Expand All @@ -89,6 +97,22 @@ def scriptUnloaded # rubocop:disable Naming/MethodName method name dictated by O
rescue => e
logger.error("Failed to call script_unloaded hook #{hook}: #{e}")
end

return if ScriptHandlingCallbacks.script_loaded

# Make sure we terminate the main thread if it's still set up, in case
# it's timing out and that's why we're unloading.
#
# It would seem simpler to just record Thread.current when this file
# loads, but if the user is using the autorequire feature of the
# jrubyscripting addon, this file will load before the main script.
#
# Note that Thread.list only includes threads that have had Ruby
# execute in them, so we don't need to worry about accidentally killing
# a random Java thread.
#
main_thread = Thread.list.find { |t| t != Thread.current && t.name.include?("-safeCall-") }
main_thread&.raise(Interrupt.new)
end

#
Expand All @@ -101,6 +125,7 @@ def scriptLoaded(filename) # rubocop:disable Naming/MethodName method name dicta
rescue => e
logger.error("Failed to call script_loaded hook #{hook}: #{e}")
end
ScriptHandlingCallbacks.script_loaded = true
end
end
end
Expand Down

0 comments on commit fb8fe61

Please sign in to comment.