Repository navigation
Stop to_json and Dynamic serialization at shared reference loops - #1222
Conversation
|
Would arrays be subject to the same issue? |
|
Yes, an array closes the loop the same way. On main, There's no to_json for arrays in the stdlib, so arrays only get there as a value inside a map or through serde. The check in this PR sits on the Shared arm, not on Map, so it already covers them. I ran those array cases against this branch: the built-in formatter writes null where the loop closes, and serde returns the reference loop error (ERROR from to_json under metadata). Happy to add an array case to the tests too. |
|
There are some unused warnings and also merge conflicts. See if you can fix them, and I"ll merge! |
e03577a to
8f196b4
Compare
|
Rebased on main and fixed the CHANGELOG conflict. The entry now links #1222, same as the other lines there. The two codegen failures were the ui_tests stderr outputs, which your 0dc3795 already fixes, so they should go green after the rebase. On the warnings, I couldn't find any from this change. cargo check on the lib is clean for default, serde/metadata, sync, no_closure, no_index, no_object, no_function and a few combinations of those. The test builds don't warn in maps.rs or the new test in serde.rs either. The unused warnings in the CI logs for this PR are in tests/closures.rs, tests/stack.rs, tests/var_scope.rs and tests/grain/limits.rs (plus the serde.rs imports on no_index/no_object), and main has all of them too. If you had a different warning in mind, tell me which feature set and I'll fix it. |
A closure stored in the object map it captures makes a reference loop, and
to_jsonfollows it with no check:mholdsf, andfcarriesmas a curried shared value. In the first callto_jsonholds the write borrow ofm, solocked_read(v).unwrap()informat_dynamic_as_jsonhitsNone. The second takes only read borrows, so the formatter recurses until the stack runs out, and neither an engine limit norcatch_unwindcan stop that. Undermetadata,to_jsongoes throughimpl Serialize for Dynamic, which fails the same two ways (cell.borrow()panics with "RefCell already mutably borrowed", or the same unbounded recursion). That also covers hosts callingserde_json::to_stringon a value returned by a script.Both serializers now pass down the chain of shared values currently being formatted, the same idea as the visited set the
Display/Debugimpls ofDynamicalready use for loops. A shared value that is already on the chain, or that cannot be read-locked, is written asnullby the built-in formatter, and serde gets an error, whichto_jsonalready turns into"ERROR". The chain follows the current path rather than recording every value seen, so a shared value that appears several times without a loop (#{a: m, b: [m]}) still serializes in full. On the serde side&Dynamicis wrapped in a small privateDynamicSer, becauseSerialize::serializehas no room for extra state; the public API is unchanged. Undersyncthe shared arm now useslocked_readlike the rest of the engine instead of a blockingread().unwrap().Added
test_map_json_shared_looptotests/maps.rsandtest_serde_ser_shared_looptotests/serde.rs. Without the fix the first panics and the second overflows the stack; with it both pass.cargo test --testspasses with default features and with thedecimal,sync,unchecked,no_closure,no_index,no_object,no_functionandcompact_strrows of the CI matrix (each withserde,metadata,internals,debugging,grain), plus the combinedsync,...,no_closure,...,uncheckedrow.cargo fmt --checkand the CI clippy invocation are clean, and both theno_stdsample and--no-default-features --features no_std,serde,syncbuild. CHANGELOG line under 1.27.1's bug fixes.Found by building nested and self-referencing values in scripts and passing them to the standard-library functions.