Repository navigation
Unify Ractor copy serialization - #18605
Merged
Merged
Conversation
jhawthorn
marked this pull request as ready for review
September 2, 2026 23:57
jhawthorn
force-pushed
the
ractor_copy_serialization
branch
2 times, most recently
from
September 14, 2026 23:48
8a05753 to
ee068fe
Compare
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
force-pushed
the
ractor_copy_serialization
branch
from
September 17, 2026 03:23
fe76788 to
c778d0e
Compare
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.dumpfor 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 sendingRactor::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_dumphooks 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.