Skip to content

Allow to use Concurrent::Map#compute_if_absent in a Ractor#1109

Merged
bensheldon merged 1 commit into
ruby-concurrency:masterfrom
Shopify:ec-concurrent-map
Jul 9, 2026
Merged

Allow to use Concurrent::Map#compute_if_absent in a Ractor#1109
bensheldon merged 1 commit into
ruby-concurrency:masterfrom
Shopify:ec-concurrent-map

Conversation

@Edouard-chin

@Edouard-chin Edouard-chin commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Allow to use Concurrent::Map#compute_if_absent in a Ractor

Problem

It is currently not possible to use the compute_if_absent method in a Ractor because there is a NULL object used solely as a sentinel that isn't shareable.

Ractor.new do
  map = Concurrent::Map.new
  map.compute_if_absent(3) { }
end

# Ractor::IsolationError:
#   can not access non-shareable objects in constant Concurrent::NULL by non-main ractor.

Solution

I'd like to simply freeze the object being hold in the NULL constant.

- ### Problem

  It is currently not possible to use the `compute_if_absent` method
  in a Ractor because there is a NULL object used solely as a sentinel
  that isn't shareable.

  ```ruby
  Ractor.new do
    map = Concurrent::Map.new
    map.compute_if_absent(3) do |map, key|
    end
  end

  # Ractor::IsolationError:
  #   can not access non-shareable objects in constant Concurrent::NULL by non-main ractor.
  ```

  ### Solution

  I'd like to simply freeze the object being hold in the `NULL`
  constant.

@bensheldon bensheldon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks correct to me 👍

Would a release sooner rather than later be helpful for Ractor work?

@bensheldon bensheldon merged commit 0f83a98 into ruby-concurrency:master Jul 9, 2026
17 checks passed
@Edouard-chin

Copy link
Copy Markdown
Contributor Author

Thanks for the offer ! This would be super helpful to get a release sooner than later. I'm not seeing any other Ractor fixes for concurrent ruby (we have a backlog of ractor safety issues in a few gems but this was the only in this gem for our current project scope).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants