Skip to content

x.async: add structured concurrency helpers#27029

Merged
medvednikov merged 2 commits intovlang:masterfrom
GGRei:async-runtime-helpers
May 1, 2026
Merged

x.async: add structured concurrency helpers#27029
medvednikov merged 2 commits intovlang:masterfrom
GGRei:async-runtime-helpers

Conversation

@GGRei
Copy link
Copy Markdown
Contributor

@GGRei GGRei commented Apr 29, 2026

This PR introduces x.async, a small structured concurrency helper module built only on existing V primitives.

I originally wrote this kind of tooling for my larger V projects, where I needed a simple and explicit way to coordinate tasks, cancellation, timeouts, worker limits, and error propagation. The module now feels mature enough to share as an experimental x. module, while keeping the design intentionally modest and easy to review.

What it provides:

  • Group for running related jobs with first-error propagation and cooperative cancellation.
  • Task[T] for one value-returning background computation with explicit wait().
  • Pool for bounded concurrent work with non-blocking try_submit() backpressure.
  • every() for blocking periodic work controlled by context cancellation.
  • with_timeout() helpers for bounding cooperative jobs.
  • Examples, integration tests, benchmarks, and validation scripts.

What it deliberately does not provide:

  • no new runtime;
  • no scheduler changes;
  • no event loop;
  • no async/await syntax;
  • no hidden background task system;
  • no panic recovery;
  • no promise chaining;
  • no multi-consumer futures;
  • no detached ticker object in this first version.

The current API is deliberately conservative:

  • cancellation is cooperative;
  • Task.wait() is one-shot;
  • Group keeps first-error behavior simple;
  • Pool.try_submit() is explicit and non-blocking;
  • periodic work is blocking and lifecycle-driven by context.

The implementation is only a documented layer over existing V tools like spawn, sync.WaitGroup, sync.Mutex, channels, context, and time.

I also added examples and integration-style tests for selected V modules such as net.http, net.websocket, veb, and mcp, while keeping them local and synthetic so they do not depend on external services, ports, or fragile server shutdown behavior.

Future additions could be discussed separately if maintainers think the direction is useful, for example:

  • blocking or timeout-based pool submission;
  • detached periodic handles;
  • higher-level helpers for server-style code;
  • optional error collection helpers;
  • more examples/tests around existing V modules that already use concurrency internally;
  • possible future adaptation of some current v modules to use x.async, if maintainers decide that would be valuable.

The goal is not to replace V’s concurrency model, but to provide a small, predictable, well-tested helper layer for common coordination patterns.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

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: a664ec8dd8

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread vlib/x/async/timeout.v Outdated
Comment on lines +56 to +57
if err.msg() == context_deadline_exceeded {
return error(err_timeout)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve parent deadline cancellation error

When with_timeout_context exits via done_ch, it unconditionally remaps context deadline exceeded to async: timeout. That also catches the case where the parent context deadline fires first, so callers cannot distinguish an upstream deadline from this helper's own timeout. This contradicts the documented behavior that parent cancellation errors are returned as-is and can break retry/telemetry logic that treats upstream deadline expiry differently.

Useful? React with 👍 / 👎.

@GGRei GGRei marked this pull request as draft April 29, 2026 15:21
@GGRei GGRei marked this pull request as ready for review April 29, 2026 15:24
@GGRei
Copy link
Copy Markdown
Contributor Author

GGRei commented Apr 30, 2026

Ready for review / integration. Codex p2 fixed.

@medvednikov medvednikov merged commit 15fb60b into vlang:master May 1, 2026
49 of 81 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.

2 participants