Skip to content

A move closure sent to spawn_os keeps its environment in the caller's frame: a captured Drop value is destroyed on the creator thread after join, and one consumed inside the closure is dropped twice (D62, §12.4) #1605

Closed

Description

@ehartford

Found while writing the modeled-C stage 9 thread fixtures (thread send drop_any_thread, spec §16.2b.10).

Spec: §12.4 / D62 — "A move || closure owns its environment … a heap cell owned by the value, freed when the closure drops, captured Drop values destroyed then — never the caller's frame."

Repro (with run --debug-alloc):

use std.thread
type P { n: i32, v: Vec[i32] }
impl Drop for P:
    move fn drop(): print("drop P")
fn main:
    let d = P { n: 1, v: Vec.new() }
    let h = spawn_os(move () => d.n)
    assert(join(h) == 1)
    print("done")

Expected: drop P before done (the environment moved to the thread; the value is destroyed when the closure drops). Actual: done then drop P — the environment lives in main's frame and d is destroyed on the creator thread after join, which makes send + drop_any_thread unobservable at runtime (a facade resource sent to a thread is still closed on the creator thread).

Consuming the capture inside the closure drops it twice:

    let h = spawn_os(move () => { let mine = d; mine.n })

prints drop P twice (once in the thread, once when main ends). With a facade resource (*mut db freed by db_close) that second drop is an invalid free under the debug allocator.

Same class as #1481 (double free of a captured non-Copy local) and #1567 (environment in the dead callee frame); this is the OS-thread manifestation. Workaround in test/behavior/behav_c_facade_thread_send.w: the closures only read their captures and the close is asserted after the frame ends.

Activity

  1. ehartford commented on Sep 26, 2026

    @ehartford
    ContributorAuthor

    Fixed on main (D62/D63 owned environment): the repro prints drop P before done, and the consuming form drops once, leak count=0 under --debug-alloc on the pinned seed; pinned by test/debug_alloc/da_closure_move_env_spawn_os.w. Remaining closure holes tracked under #1698.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions