Interrupt or abort generation¶
A generating node must cooperate with cancellation. Declare its modality with a generator mixin such as TextGenerator or AudioGenerator, choose its cancellation strategy, check the active call's token, and implement the state cleanup required by its disposition.
Choose the disposition¶
| Disposition | Partial output/state | Pending input |
|---|---|---|
Disposition.INTERRUPT |
Retain | Retain |
Disposition.ABORT |
Discard | Discard |
The disposition describes the generator's contract. It does not retract bytes already received by a client or sound already played by a device. Do not equate cancelling a local asyncio task with completing this runtime contract.
Run the text and audio checks¶
Use the MS2 native profile:
Keep generator_node.py, node_transform.py, and worker_session.py alongside the launchers.
The deterministic text generator declares CancelStrategy.OBSERVES_TOKEN. Each call captures self.cancel_token, checks it between chunks, and yields every 20 ms. Its cancel(call, cancellation) hook commits the partial turn only for retain semantics. It exposes generation_state() to verify which hook ran and what history remains.
The text check first runs assert_generator_cancels_async(..., stateful=True) against the in-memory worker-loop harness. It then repeats interrupt and abort in real host-managed workers, verifies the disposition, checks that no output arrives after the 250 ms test allowance, and proves that the next call sees the expected history. In-memory conformance alone would not establish Rust-host behavior.
For an already attached control client, this excerpt injects a barge-in carrying the intended disposition:
from remotemedia.control import Data
from remotemedia.core.cancel import Disposition
async def interrupt(ctrl):
await ctrl.publish("worker.in.barge_in", Data.from_json({
"disposition": Disposition.INTERRUPT.to_dict(),
}))
The audio check generates 24 kHz mono float32 chunks of 480 samples, cancels after the first chunk, and observes the FFI receiver for 350 ms. It asserts zero samples received later than 250 ms after the cancellation request. This is a bounded synthetic transport check, not a maximum-latency guarantee, a speaker playback test, or a listening evaluation.
Keep cancellation responsive¶
The example yields to the event loop between chunks. Long blocking model work cannot observe the token while it owns the thread; use a model's supported stopping mechanism or a suitable execution strategy. Preserve upstream model controls and document what cannot be interrupted.
Use separate mechanisms for a reply deadline, stopping generation, closing an attach, and closing a session. The examples drain their next text call before session close and stop receiver tasks explicitly. The current Windows native profile can still log a copy-accounting shutdown error; the retained limitation is not evidence that cancellation failed or that accounting closed successfully.
Source references: remotemedia-sdk/clients/python/remotemedia/core/cancel.py · remotemedia-sdk/clients/python/remotemedia/testing/cancellation.py (base revision; reviewed with local changes) · remotemedia-sdk/clients/python/remotemedia/core/multiprocessing/node.py · remotemedia-sdk-base/src/transport/session_control.rs (base revision; reviewed with local changes)