Skip to content

Unify Ractor copy serialization - #18605

Merged
jhawthorn merged 6 commits into
ruby:masterfrom
jhawthorn:ractor_copy_serialization
Sep 17, 2026
Merged

jhawthorn merged 6 commits into
ruby:masterfrom
jhawthorn:ractor_copy_serialization

Conversation

@jhawthorn

@jhawthorn jhawthorn commented Sep 2, 2026 •

Copy link
Copy Markdown
Member

This unifies the two methods which objects are copied. Since Ractor-local GC this has used "native" (now via ractor_courier) copies for a set of known types and shareable objects, and used Marshal.dump for other objects.

This caused issues and confusing results because it applied to the entire object sent. When an sending a combination of native-only and marshal-only objects, like ractor.send([Time.now, Ractor::Port.new]) this would have to take the marshal path (because of Time) which doesn't support sending Ractor::Port. Marshal is intended for serializing to disk/network rather than within the same process.

This PR proposes solving this by removing the Marshal path, and instead calling marshal_dump/_dump/_data_dump hooks from the sender, and serializing the returned payload as other objects would be. This should be a little faster (we don't need to go all the way to the string marshal format) and more importantly .

The first commit fixes an existing bug where objects (notably hashes inside hashes) may not have been loaded in the right order. This becomes more necessary to use this marshal-hook-based loading, since we need the objects to be loaded before we marshal load them.

This also uses the registered Marshal "compat" functions, which are necessary for some types like Set.

This was assisted by claude code.

@jhawthorn jhawthorn changed the title Ractor copy serialization Unify Ractor copy serialization Sep 2, 2026
@jhawthorn
jhawthorn marked this pull request as ready for review September 2, 2026 23:57
@jhawthorn
jhawthorn force-pushed the ractor_copy_serialization branch 2 times, most recently from 8a05753 to ee068fe Compare September 14, 2026 23:48
Previously rb_ractor_courier_materialize filled every non-hash node in
id order and then inserted hash entries in a separate pass in reverse
id order, on the assumption that ids are assigned depth-first so a key
always has a larger id than the hash holding it. That isn't true for a
shared key: an object reached earlier in the graph keeps its lower id,
so a hash referencing it later inserted the key while it was still an
empty shell and recorded the wrong #hash for it.

    key = { 1 => 2 }
    copy_key, copy_hash = Ractor.new { Ractor.receive }.send([key, { key => 1 }]).value
    copy_hash[key] # => nil, should be 1

This records the order each node finishes capturing (children before
parents) and materializes in that order, so a node is only filled once
everything below it is complete. That also removes the need for the
separate hash pass. Only a cycle can still reach a node that is being
filled, which was already out of scope.
Previously any Regexp objects being sent would cause us to take the
Marshal path.
Previously the courier only carried the core types natively, and any
other object made the whole message fall back to Marshal.dump on the
sender and Marshal.load on the receiver. That serialized everything
reachable into bytes, so a Ractor::Port or another shareable object
anywhere in the graph couldn't be sent alongside, for example, a Time.

This teaches the courier the same protocols Marshal uses: marshal_dump
with marshal_load, _dump with klass._load, and _dump_data with
_load_data, chosen in Marshal's order of preference. The hook runs on
the sender as before, but instead of being serialized its return value
is captured as an ordinary child node. Sharing, cycles and shareable
references all survive the same way they do for any other node, and
every reference to the same object resolves to the one copy on the
receiver.

Only copy uses this. Move would have to take the source apart, which a
dump hook can't do, so it still refuses these objects.

The preflight in copy_courier_supported_p doesn't walk what the hook
returns, since running it twice isn't an option, so nodes for the
payload are allocated through the growth path rather than the
reservation.
For some types (probably most notably Set), Marshal loading is defined
with compatibility functions. We should use those when set.
The previous commit integrated Marshal's hooks into the native courier
copying path. That should cover most cases.

One exception I've noticed that this doesn't implement is singleton
classes which have been extended by modules. I would rather we implement
that separately if we find it to be an issue.
Previously the Ractor courier rejected singleton class objects. However
we have existing tests of OpenStruct that those objects can be
Ractor-copied.

dup and clone, which were used prior to RLGC's merge, also just ignore
singleton classes, so I believe this is in line with existing behaviour.
@jhawthorn
jhawthorn force-pushed the ractor_copy_serialization branch from fe76788 to c778d0e Compare September 17, 2026 03:23
@jhawthorn
jhawthorn merged commit ab49f88 into ruby:master Sep 17, 2026
163 of 170 checks passed
@tikkss

tikkss commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

Thanks for your PR!!!

We met the same issue while working on a Ractor-based parallel test runner in test-unit.

This will allow us to remove our workaround.

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