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

Fix defining object finalizer in Ruby 3.1 to reference a UUID string #74

Merged
merged 8 commits into from
Aug 28, 2023
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log

### Fixed
* Fix defining object finalizer in Ruby 3.1 to reference a UUID string. by Vadim Kononov(@vkononov)

## [v0.14.0] - 2020-09-12

### Added
Expand Down
17 changes: 12 additions & 5 deletions lib/finite_machine/observer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "securerandom"
vkononov marked this conversation as resolved.
Show resolved Hide resolved

require_relative "async_call"
require_relative "callable"
require_relative "hook_event"
Expand All @@ -16,9 +18,10 @@ class Observer < GenericDSL
# Clean up callback queue
#
# @api private
def self.cleanup_callback_queue
def cleanup_callback_queue
proc do
begin
ObjectSpace.undefine_finalizer(@object_id)
if callback_queue.alive?
callback_queue.shutdown
end
Expand All @@ -40,15 +43,19 @@ def self.cleanup_callback_queue
#
# @api public
def initialize(machine)
@machine = machine
@hooks = Hooks.new
@machine = machine
@hooks = Hooks.new
@object_id = nil

@machine.subscribe(self)
ObjectSpace.define_finalizer(self, self.class.cleanup_callback_queue)
end

def callback_queue
@callback_queue ||= MessageQueue.new
return @callback_queue if instance_variable_defined?(:@callback_queue)

@callback_queue = MessageQueue.new
@object_id = SecureRandom.uuid
ObjectSpace.define_finalizer(@object_id, cleanup_callback_queue)
Copy link

@abhishekjain16 abhishekjain16 Aug 7, 2023

Choose a reason for hiding this comment

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

@vkononov This method needs to return @callback_queue

      return @callback_queue if instance_variable_defined?(:@callback_queue)

      @callback_queue = MessageQueue.new
      @object_id = SecureRandom.uuid
      ObjectSpace.define_finalizer(@object_id, cleanup_callback_queue)
      @callback_queue

Add @callback_queue to return from this method fixes pipeline

end

# Evaluate in current context
Expand Down