Skip to content

The guide does not document the native C side of custom effects #825

Closed

Description

@luccahuguet

What I did

The guide says that each effect may import a .c and .js implementation and that users can add effects the same way as Base. I used that path for a native program whose custom effects own resources and park on file descriptors.

The native effect path works on Bend 2.0.7 after I reused Base handle types, but I had to derive the C interface from comp.ts, the Base effect sources, and the IO tests. The guide specifies the generated host-function name and stops there.

Missing native documentation

A native custom effect must answer questions that the guide does not cover:

  • Which C function signature and registration call should an imported effect use?
  • Which result constructors may it return, including io_fail with custom text?
  • Which IoWork fields may it own across io_work or io_wait_on, and who releases their memory after success or failure?
  • Can a package rely on io_eff, io_hand, io_hand_v, io_work, and io_wait_on, or are those helpers private compiler details?
  • Does Bend support a nominal opaque handle type for a custom resource?
  • What compatibility, if any, does Bend promise for imported C across releases?

The handle question has no documented answer. This declaration follows the guide's affine-handle description and compiles:

import Base

type Handle is Type:

def Handle.open() -> IO(Result<&1, &1, U32 & String, Handle>):
  import "./handle.c"

def Handle.close(handle: Handle) -> IO(Unit):
  import "./handle.c"

def main() -> IO(Unit):
  do IO<Unit>:
    handle : Handle <- IO.try(Handle, Handle.open())
    Handle.close(handle)

handle.c:

static Term handle_open_run(Env e, Term* f, IoWork* w) {
  return io_done(e, io_hand(42));
}

static Term handle_close_run(Env e, Term* f, IoWork* w) {
  fprintf(stderr, "handle=%llu\n", (unsigned long long)io_hand_v(f[0]));
  return term_pak(CID_UNIT, 0);
}

static void __attribute__((constructor)) handle_effects_use(void) {
  io_eff(CID_HANDLE_OPEN, handle_open_run, 0);
  io_eff(CID_HANDLE_CLOSE, handle_close_run, 0);
}
$ bend main.bend -o main && ./main
handle=0

Handle.close receives a zero word rather than 42. An unfilled law Handle: Type fails with 1 TODO found. The working fallback I found defines Handle as File, Socket, or Listener, which loses nominal separation between unrelated resources.

This probe does not assume that an empty datatype should carry a foreign word. It shows that the guide does not name a supported custom-handle mechanism or state that none exists.

Requested documentation

Please document the native C side of custom effects, or state that packages must treat it as unsupported compiler internals. A small checked-in example or a guide link to an existing test would be enough. It should cover:

  1. one synchronous effect and its registration;
  2. the supported value and error representations;
  3. the supported pattern, if any, for an opaque affine resource handle;
  4. one worker or file-descriptor park/resume path and its memory ownership;
  5. the compatibility policy for imported C across Bend releases.

This request does not require a stable C ABI. Documentation may state that a custom effect must track its exact Bend compiler version.

References

Environment

bend 2.0.7 (b4929c2d0d9d1e9598954aa37758954bab4d8a77)
Linux x86_64
Bun 1.3.13
Ubuntu clang 18.1.3

Activity

  1. VictorTaelin commented on Sep 19, 2026

    @VictorTaelin
    Contributor

    2.0.13 adds bend guide effects (guide/EFFECTS.md): the effect def, the C side (signature, io_eff and the three needs, io_done/io_fail/io_hand, io_work and io_wait_on, who owns w, f and w->data), the JS side, a complete example on both lanes, and the compatibility policy (the C side tracks the exact compiler version; no ABI promise). It is AI-written from the runtime's source and a human will revise it; report anything wrong. Your second finding, that a user cannot declare a handle type (a user law Handle: Type counts as open; only Base's handle laws are exempt), is now a WONTFIX DESIGN entry: opening it touches consistency, and we will address the gap later.

    Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.

  2. luccahuguet commented on Sep 19, 2026

    @luccahuguet
    Author

    I checked the new C-effects guide against a resource-owning native effect and found one cleanup caveat to document.

    Our effect opened a Unix listener and PTY child. In a disposable Bend 2.0.7 build on x86_64 Linux, I forced err_fail after both resources existed. Bend exited 1, left the socket pathname, and left a child that ignored SIGHUP running. At the same point, returning io_fail(EIO) let our atexit cleanup reap the child and remove the socket (exit 5).

    err_fail still calls _exit(1) in comp.ts at 01e13b72. Could guide/EFFECTS.md state that this fatal path bypasses atexit cleanup? Authors of resource-owning effects need that boundary when they choose their cleanup strategy. I am not asking to change fail-stop behavior or reopen this issue.

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