Skip to content

fix(ffi): a Kotlin call closes the handles it moved, not those its argument holds after - #940

Open
kixelated wants to merge 2 commits into
mainfrom
quest/m1/ffi-export/kotlin-moved-snapshot
Open

kixelated wants to merge 2 commits into
mainfrom
quest/m1/ffi-export/kotlin-moved-snapshot

Conversation

@kixelated

@kixelated kixelated commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

A Kotlin call that moves objects out of a collection argument, such as logCounts's more: List<Counter>, now closes exactly the handles it encoded, whether it returns or unwinds, even when a callback it reaches changes that collection meanwhile. Before, the cleanup walked the argument again after the call, so a callback that swapped A for B abandoned B, which Rust never took, and left A's wrapper open on a value Rust had already taken.

Changes

  • ffi/runtime/rt.kt: __Writer.moved(handle) encodes a handle as handle does and marks it in a BooleanArray beside held. __Writer.abandon() abandons each marked handle. Nothing is copied per call: the array is allocated only once a moved handle is encoded, and grows with slots.
  • ffi/src/kotlin.rs: Gen::encode is write_value with a moves flag. A moved argument's writes emit w.moved(...) for each non-Copy object reached by value, through a list, Option, Box, or tuple, never through a reference. Its after is w.abandon(). Gen::close, which re-walked the argument, is gone.
  • ffi/tests/api.kt: in unwinds(), a Log callback replaces more[0] during logCounts. The test checks that the moved counter is closed and the replacement stays open, once on the success path and once on the unwind path, where a callback's failed char check unwinds the call.

Decisions

  • Mark moved handles in the writer instead of collecting a separate snapshot list: the writer already holds every encoded handle in held, so a parallel BooleanArray is all the extra state needed. This is the Plan's second option.
  • A Copy object inside a moved tuple, such as the entry tuple's second element, is still encoded with handle and is never abandoned, as before.

Validation

  • Reproduced first: with the new tests/api.kt on the old generator, just ffi test --test fixtures api_kotlin -- --exact fails with failed: what the call moved is closed. With the fix it passes.
  • Read the generated Kotlin for the api fixture. Every moved list or tuple argument (logCounts, counters, chain, items, entry, ...) writes wN.moved(...) and runs wN.abandon() on both paths. The Copy element of entry's tuple is still written with wN.handle(...).
  • just ci passes locally.

Public API

None. The generated Kotlin's public signatures and the C ABI are unchanged; only _Native.__Writer's internal runtime gains moved and abandon.

Caused by

  • kixel-ai/rust2#190 - closed a moved Vec<T> argument's handles by walking the argument after the call, not by the handles encoded (found by reviewer: CodeRabbit on kixel-ai/rust2#922, which runs the same cleanup on the unwind path)

Follow-ups

None.

Models

Size: S
Implementer: Claude Code, claude-opus-5-5, medium effort (arm A)

🤖 Generated with Claude Code

Review

Rewritten by quest review after each round.

  • Head: 1c4f56a

  • Base: origin/main at b99b71f

  • Risk: medium, the base has no RISK.md to name what is always high or customer-facing, so every change is medium

  • Classified: medium by default

  • Reviewer: Codex, gpt-6.1-sol, medium effort

  • Session: 01a1fcc4-5b63-7c10-8c9f-61571183b539, round 1

  • Score: 9/10, The writer preserves replacement handles across successful and unwinding callbacks, with regression coverage and no actionable defects found by inspection, though tests were not run as instructed.

  • Rounds: 1; first round 0; findings 0: fixed 0, refuted 0, open 0

  • Findings: none

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of values transferred through Kotlin callbacks. When a callback replaces an item in a transferred collection or encounters an error, the original value is now closed correctly, while the replacement remains available. This also applies when the callback fails during character conversion.

@coderabbitai

coderabbitai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The Kotlin FFI writer now records moved handles during argument encoding. Generated calls abandon those recorded handles after execution. Regression tests cover callbacks that replace a moved list element, both with successful returns and exceptions.

Changes: Kotlin FFI moved-handle cleanup

Layer / File(s) Summary
Track moved handles in the writer
ffi/runtime/rt.kt
__Writer records the indices of moved handles and abandons only handles marked as moved.
Generate move-aware encoding and cleanup
ffi/src/kotlin.rs, ffi/tests/api.kt, quest/m1/ffi-export/kotlin-moved-snapshot.md
Argument encoding propagates move tracking through lists, options, boxes, and tuples. Generated calls schedule __Writer.abandon() after execution instead of generating recursive per-value cleanup. Tests cover callbacks that replace a moved list element and callbacks that throw. The snapshot note was removed.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Possibly related PRs

  • kixel-ai/rust2#750: Introduces Kotlin cleanup for moved arguments. This change replaces the re-walk cleanup in that path with cleanup based on handles marked as moved.

Suggested reviewers: pullfrog

Merge Risk: Minimal · up to 1c4f5

This change makes Kotlin FFI cleanup close the handles Rust actually took, rather than any replacement objects a callback put in the list. No concrete defect was identified, and the regression tests cover both normal return and unwind, so the merge risk is minimal.

Security architecture risk: Minimal · up to 1c4f5

The change ties cleanup to the handles actually transferred, rather than to a collection that callbacks may modify. The inspected paths preserve pre-call rejection and post-call cleanup behavior, with no introduced or worsened ownership or security risk identified.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The affected ownership boundary is process-local Kotlin/JNI-to-Rust handle transfer. The change applies to generated calls with encoded moving object arguments, not just the logCounts fixture; the relevant outcome is wrapper validity and native resource lifecycle.

Trust Boundaries and Controls

  • observed — Callback validation failures are distinguished from pre-transfer rejection: the generated bridge wraps invalid callback results as __Unwound, which the native bridge propagates through Rust frames. That exception does not match the cleanup-suppression conditions, preserving abandonment after transfer. Other callback exceptions retain the existing abort behavior.

Resilience and Maintainability Implications

  • observed — Movement state belongs to a fresh writer for each encoded argument rather than a global registry. Handle abandonment uses the existing atomic CLOSED transition and returns when already closed, making repeated abandonment idempotent while outstanding holds govern final release.
Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: Kotlin FFI calls now close only the handles they moved, not handles the argument holds after the call. The raw summary confirms this change: moved handles are marked d…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: Kotlin FFI calls now close only the handles they moved, not handles the argument holds after the call. The raw summary confirms this change: moved handles are marked during encoding and abandoned after return or unwind, instead of re-walking the argument. The title is specific and matches the objectives, which describe callback-driven list mutation causing cleanup to close the wrong handles.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4b3c608e-124b-44d6-a15d-fe0967cb03d2
Commits

Reviewing files that changed from the base of the PR and between 7d7b3f6 and 1c4f56a.

Files selected for processing: 4

  • ffi/runtime/rt.kt
  • ffi/src/kotlin.rs
  • ffi/tests/api.kt
  • quest/m1/ffi-export/kotlin-moved-snapshot.md
Files with no reviewable changes: 1

  • quest/m1/ffi-export/kotlin-moved-snapshot.md

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review.

…gument holds after

The writer marks each handle it encodes whose value the call takes, and abandons those
once the call returns or unwinds, instead of walking the argument again: a callback that
changed a list argument meanwhile no longer closes the wrong handles.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title Kotlin: a call closes the handles it moved, not those its argument holds after fix(ffi): a Kotlin call closes the handles it moved, not those its argument holds after Nov 21, 2026
@kixelated
kixelated marked this pull request as ready for review November 21, 2026 06:22

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ No new issues found.

Approach: sound. The writer marks which handles it encoded as moved, so the cleanup closes exactly what Rust took. This fixes the cause, not a symptom, and adds no per-call copy.

Reviewed changes — the full diff of both commits: the runtime writer, the generator, the regression test, and the quest file removal.

  • __Writer.moved / abandon: moved encodes the handle through handle() and records its index in a lazily allocated BooleanArray that grows with slots. abandon() closes only the marked held entries. The index bound holds because handle() always grows slots beyond held.size - 1.
  • Gen::encode: moves propagates through List, Option, Box and Tuple, but not through Ref. Objects get w.moved(...) only when moved(abi, ty) is true, so Copy objects inside moved tuples still use handle and are never abandoned. This matches the old close filter.
  • Cleanup placement: {w}.abandon() goes into after, which Gen::moving runs inside the writer's use {} on both the return and unwind paths. It runs before release(), the same order as the deleted Gen::close.
  • Regression test: a Log callback swaps changed[0] during logCounts. The test checks that the moved counter is closed and the replacement stays open, on both the success path and the unwind path (a lone-surrogate char check).

ℹ️ Go's binding still finds moved handles by re-walking the argument

This PR does not touch Go, but Go has the same pattern. go.rs close builds for _, v := range more { v.Handle_.Moved() } and runs it after the call (moves.push(self.close(&param.ty, &name, 0)) near ffi/src/go.rs:2992). A Go slice shares its backing array with the caller, so a callback that captured more and sets more[0] = placed would cause the same mismatch fixed here: placed gets marked moved and the taken object stays open. Swift's close also re-walks, but Swift arrays are value types, so a callback cannot change the caller's copy. This is mergeable as is; a follow-up quest for Go seems worthwhile.

Technical details
# Go: a call marks moved the handles its slice argument holds after the call, not those it encoded

## Affected sites
- ffi/src/go.rs:2792 — `close` re-walks `e` (slices, options, tuples) after the call
- ffi/src/go.rs:2992 — `moves.push(self.close(&param.ty, &name, 0))`

## Required outcome
- A Go call that moves objects out of a slice argument marks moved (`Handle_.Moved()`) exactly the handles it encoded, even when a callback reassigns slice elements during the call; the replacement stays open.
- Regression test in the Go api test, mirroring `unwinds()` in ffi/tests/api.kt.

## Suggested approach
- Collect the moved handles while lowering (as Kotlin's `__Writer.moved` now does), then run `Moved()` on that list.

Pullfrog  | View workflow run | Using claude-opus-5-5 | 𝕏

This branch has not been deployed

No deployments
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.

1 participant