fix(worker): report worker-side validation and STA shutdown without an MXAccess HRESULT (WIP-005, #255)
Per D9: a request the worker rejects before calling MXAccess (write value with no kind, raw payload, empty MxArray) throws the new WorkerRequestValidationException and replies INVALID_REQUEST with no HRESULT. A plain ArgumentException stays MXACCESS_FAILURE with its HRESULT, because the interop raises MXAccess E_INVALIDARG that way. The dispatcher now converts executor exceptions on the STA, so anything the InvokeAsync await throws is the runtime refusing the work item (shutdown, disposal, dead loop, canceled at shutdown) and replies WORKER_UNAVAILABLE. No .proto change. Status mapping documented in docs/WorkerConversion.md.
This commit is contained in:
+18
-20
@@ -129,38 +129,36 @@ The MXAccess engine returns values whose semantic type only fully resolves after
|
||||
|
||||
## HResultConverter and HResultConversion
|
||||
|
||||
`HResultConverter.Convert` wraps any `Exception` thrown across the COM boundary. It prefers `COMException.ErrorCode` over `Exception.HResult` because the runtime sometimes overwrites `Exception.HResult` while marshalling, and the `ErrorCode` field is the value the COM call actually returned.
|
||||
`HResultConverter.Convert` turns an exception the command executor threw into a reply status. It prefers `COMException.ErrorCode` over `Exception.HResult` because the runtime sometimes overwrites `Exception.HResult` while marshalling, and the `ErrorCode` field is the value the COM call actually returned.
|
||||
|
||||
```csharp
|
||||
public HResultConversion Convert(Exception exception)
|
||||
{
|
||||
if (exception is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(exception));
|
||||
}
|
||||
### Status mapping
|
||||
|
||||
int hresult = exception is COMException comException
|
||||
? comException.ErrorCode
|
||||
: exception.HResult;
|
||||
The reply status says who failed. A client must be able to tell a request it built wrongly, a worker that is going away, and MXAccess rejecting the call apart, and an HRESULT is reported only when MXAccess produced one.
|
||||
|
||||
return new HResultConversion(
|
||||
hresult,
|
||||
CreateProtocolStatus(hresult, exception),
|
||||
CreateSafeDiagnosticMessage(exception));
|
||||
}
|
||||
```
|
||||
| Failure | `ProtocolStatusCode` | `hresult` |
|
||||
|---------|----------------------|-----------|
|
||||
| `WorkerRequestValidationException`: the worker rejected the request before any MXAccess call (a write value with no kind, a raw payload, an `MxArray` with no values) | `INVALID_REQUEST` | not set |
|
||||
| The STA runtime refused or dropped the work item: shut down, disposed, the STA loop died, or a queued item was canceled at shutdown | `WORKER_UNAVAILABLE` | not set |
|
||||
| `COMException` from an MXAccess call | `MXACCESS_FAILURE` | `COMException.ErrorCode` |
|
||||
| `ArgumentException` from an MXAccess call (the interop raises `E_INVALIDARG`, `0x80070057`, as `ArgumentException`) | `MXACCESS_FAILURE` | `0x80070057` |
|
||||
| Any other exception from the executor | `MXACCESS_FAILURE` | `Exception.HResult` |
|
||||
|
||||
`WorkerRequestValidationException` derives from `ArgumentException` but is a separate type because a plain `ArgumentException` is ambiguous: the interop raises one for an MXAccess `E_INVALIDARG` (for example `Suspend` on an item that is added but not advised), and that one is MXAccess parity and keeps its HRESULT. Before this split a malformed write reported `MXACCESS_FAILURE` with `0x80070057`, which is the same answer MXAccess gives for a bad argument, so the operator went looking at the Galaxy attribute (WIP-005, decision D9 in the 2026-12-02 review plan). No `.proto` change was needed: both codes already existed, and clients already handle a reply without an HRESULT.
|
||||
|
||||
`StaCommandDispatcher` keeps the two failure sources apart structurally. The executor runs inside a wrapper on the STA that converts its exceptions to replies through `HResultConverter`, so any exception the `StaRuntime.InvokeAsync` await itself throws can only come from the runtime, and the dispatcher answers it with `WORKER_UNAVAILABLE`. The dispatcher's own shutdown and queue-full rejections were already `WORKER_UNAVAILABLE`.
|
||||
|
||||
`CreateProtocolStatus` maps `0` (`S_OK`) to `ProtocolStatusCode.Ok` and any non-zero HRESULT to `ProtocolStatusCode.MxaccessFailure`. The status `Message` includes the exception type name and the formatted HRESULT (`HRESULT 0x<8 hex digits>`), giving downstream operators the same identifier they would see in COM event logs.
|
||||
|
||||
`HResultConversion` is an immutable carrier with three fields:
|
||||
`HResultConversion` is an immutable carrier:
|
||||
|
||||
| Field | Purpose |
|
||||
|-------|---------|
|
||||
| `HResult` | Raw signed 32-bit HRESULT, suitable for inclusion in a wire reply |
|
||||
| `HResult` | Raw signed 32-bit HRESULT, suitable for inclusion in a wire reply; zero when `HasHResult` is false |
|
||||
| `HasHResult` | False for a worker-side validation failure, so the reply leaves `hresult` unset |
|
||||
| `ProtocolStatus` | The `ProtocolStatus` projection used in command replies |
|
||||
| `DiagnosticMessage` | A safe, fully-qualified diagnostic string for logs |
|
||||
|
||||
The diagnostic message is built from the exception type's `FullName` and the formatted HRESULT only. It deliberately omits `Exception.Message` so user-supplied or localized strings do not leak into worker log channels that may be shipped to less trusted destinations.
|
||||
For an MXAccess failure the diagnostic message is built from the exception type's `FullName` and the formatted HRESULT only. It deliberately omits `Exception.Message` so user-supplied or localized strings do not leak into worker log channels that may be shipped to less trusted destinations. A validation failure's message is the worker's own fixed text and never contains a value, so it is included.
|
||||
|
||||
## MxStatusProxyConverter
|
||||
|
||||
|
||||
Reference in New Issue
Block a user