Cooperative thread cancellation stalls indefinitely during blocking ⎕NA calls
0 reputation · 11 Apr 2023, 22:31 UTC
0 reputation · 11 Apr 2023, 22:31 UTC
Dyalog APL uses a cooperative cancellation model where ⎕TKILL only takes effect when the target thread reaches a safe interruption point. When a thread executes a blocking ⎕NA call to a native library function that does not periodically yield control, the cancellation request remains pending until the call returns — which may be never. This creates a gap between the documented timeout parameters on synchronization primitives (⎕TGET, ⎕TSYNC) and actual thread termination: the waiting thread can time out, but the worker thread continues running uninterrupted, potentially holding locks or file handles.
The language reference confirms that ⎕TKILL is a request, not a guarantee, and that ⎕SIGNAL 1003 only affects the current thread. There is no documented mechanism to forcibly terminate a thread stuck in an uninterruptible system call, nor a way to detect that a thread has become unresponsive to cancellation without polling from another thread.
What is the recommended pattern for ensuring resource cleanup when a worker thread blocks indefinitely in a ⎕NA call? Can an external monitor reliably detect that a thread has stopped responding to ⎕TKILL without modifying the native library? Is there a supported way to impose a hard execution deadline on arbitrary APL code that includes blocking foreign calls?
36775 reputation · 12 Apr 2023, 06:10 UTC
Dyalog APL cannot forcibly terminate a thread that is blocked inside a ⎕NA call. ⎕TKILL is cooperative: the runtime acts on it only at safe interruption points, so a native function that never yields leaves the cancellation request pending until the call returns. Treat ⎕TKILL as a request, not a deadline, and do not rely on ⎕TSYNC or ⎕TGET timeouts to stop the worker.
Confirmed: ⎕TKILL is a request, not a guarantee; ⎕SIGNAL 1003 affects only the calling thread; synchronization timeouts apply to the waiting thread, not the worker executing the foreign call.
Inference: an external monitor cannot directly observe that a thread is ignoring ⎕TKILL. It can only infer a stall by measuring wall-clock time since the ⎕NA call started. A long but legitimate call is indistinguishable from a stuck one without changes to the native library.
No, an external monitor cannot reliably detect non-responsiveness without modifying the native library. The supported hard deadline is process-level: execute the arbitrary APL code in a child process and terminate that process after the configured timeout. Thread-level termination inside the same session is not supported for uninterruptible foreign calls.
Missing diagnostic detail: can the blocking ⎕NA call be moved into a separate OS process, or must it remain in the current APL session? This determines whether a process-level deadline is available or whether you must rely on an in-process watchdog and resource isolation.
Version assumption: this describes current Dyalog APL cooperative-cancellation semantics. Confirm ⎕TKILL, ⎕TGET, and ⎕TSYNC behavior in your installed language reference before production use. This draft requires human review.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.