Skip to content

builtin: add closure lifetime API#19

Merged
GGRei merged 1 commit into
fix/27445-closure-context-leak-minimalfrom
fix/27445-closure-lifetime-api
Jun 18, 2026
Merged

builtin: add closure lifetime API#19
GGRei merged 1 commit into
fix/27445-closure-context-leak-minimalfrom
fix/27445-closure-lifetime-api

Conversation

@GGRei

@GGRei GGRei commented Jun 17, 2026

Copy link
Copy Markdown
Owner

Summary

This is the second phase on top of the closure-context foundation branch.

⚠️ This PR must not be merged before vlang#27483. It depends on that closure-context foundation work and should be treated as blocked until vlang#27483 is merged.

It adds an explicit, reusable closure lifetime/reclaim API that frame-based or immediate-mode code can use without exposing raw closure destruction.

Public API added:

  • closure.new_lifetime()
  • Lifetime.frame(work fn ()) !
  • Lifetime.reclaim(retain int) !
  • Lifetime.reclaim_all() !
  • Lifetime.dispose() !
  • Lifetime.suspend(work fn ()) !
  • Lifetime.untracked(work fn ()) !

The API lets a caller create a lifetime owner, run one transient frame under that owner, reclaim old frame-owned closure contexts, and explicitly keep persistent callbacks outside reclaim when needed.

Important contract: frame, suspend, and untracked borrow their work callback. The lifetime owns only the closure contexts created while a frame is active.

Tests

All tests below were run on the current PR head 3d974672d.

./v_pr19_verify fmt -verify \
  vlib/builtin/closure/closure.c.v \
  vlib/builtin/closure/closure_nix.c.v \
  vlib/builtin/closure/closure_windows.c.v \
  vlib/v/gen/c/fn.v \
  vlib/v/tests/fns/closure_lifetime_api_test.v \
  vlib/v/tests/fns/closure_context_skip_unused_test.v

./v_pr19_verify missdoc --verify vlib/builtin/closure/closure.c.v

./v_pr19_verify test vlib/v/tests/fns/closure_lifetime_api_test.v
./v_pr19_verify -gc none test vlib/v/tests/fns/closure_lifetime_api_test.v
./v_pr19_verify -gc boehm test vlib/v/tests/fns/closure_lifetime_api_test.v
./v_pr19_verify -gc boehm_leak test vlib/v/tests/fns/closure_lifetime_api_test.v
./v_pr19_verify -cstrict test vlib/v/tests/fns/closure_lifetime_api_test.v

./v_pr19_verify test vlib/v/tests/fns/closure_context_skip_unused_test.v
./v_pr19_verify -gc boehm_leak test vlib/v/tests/fns/closure_context_skip_unused_test.v

./v_pr19_verify test vlib/v/gen/c/closure_context_codegen_test.v
./v_pr19_verify -gc boehm test vlib/v/tests/fns/closure_context_boehm_root_test.c.v
./v_pr19_verify -gc none test vlib/v/tests/fns/closure_context_boehm_root_test.c.v

./v_pr19_verify -gc none test vlib/v/tests/loops/for_c_init_closure_cleanup_test.v
./v_pr19_verify test vlib/v/tests/defer/scoped_defer_test.v
./v_pr19_verify test vlib/v/gen/c/coutput_test.v

VJOBS=1 ./v_pr19_verify test vlib/v/tests/fns

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 331a098fa3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread vlib/builtin/closure/closure.c.v
Comment thread vlib/builtin/closure/closure_once_nix.h Outdated
Comment thread vlib/builtin/closure/closure.c.v Outdated
Comment thread vlib/builtin/closure/closure.c.v Outdated
@GGRei GGRei force-pushed the fix/27445-closure-lifetime-api branch from 331a098 to 9a7d1bc Compare June 17, 2026 22:02
@GGRei

GGRei commented Jun 17, 2026

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9a7d1bcade

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread vlib/v/gen/c/fn.v Outdated
Comment thread vlib/v/gen/c/fn.v
Comment thread vlib/v/gen/c/fn.v Outdated
Comment thread vlib/builtin/closure/closure_once_nix.h Outdated
Comment thread vlib/builtin/closure/closure.c.v Outdated
@GGRei GGRei force-pushed the fix/27445-closure-lifetime-api branch from 9a7d1bc to ba45dd9 Compare June 17, 2026 22:56
@GGRei

GGRei commented Jun 17, 2026

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ba45dd930f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread vlib/v/gen/c/fn.v
Comment thread vlib/builtin/closure/closure.c.v Outdated
Comment thread vlib/builtin/closure/closure.c.v
Comment thread vlib/builtin/closure/closure.c.v
@GGRei GGRei force-pushed the fix/27445-closure-lifetime-api branch from ba45dd9 to 252d4f4 Compare June 18, 2026 05:33
@GGRei

GGRei commented Jun 18, 2026

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 252d4f4ba0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread vlib/v/gen/c/fn.v
Comment thread vlib/v/gen/c/cgen.v Outdated
Comment thread vlib/builtin/closure/closure.c.v
@GGRei GGRei force-pushed the fix/27445-closure-lifetime-api branch from 252d4f4 to 53ace4c Compare June 18, 2026 06:40
@GGRei

GGRei commented Jun 18, 2026

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 53ace4c112

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread vlib/v/gen/c/fn.v
Comment thread vlib/v/gen/c/fn.v Outdated
@GGRei GGRei force-pushed the fix/27445-closure-lifetime-api branch from 53ace4c to 3d97467 Compare June 18, 2026 07:37
@GGRei

GGRei commented Jun 18, 2026

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 3d974672df

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@GGRei GGRei merged commit 3d97467 into fix/27445-closure-context-leak-minimal Jun 18, 2026
74 of 93 checks passed
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