Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/mstest-runner-protocol/001-protocol-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ An example of an error:
}
```

An error response must include `id`, but its value is `null` when the peer could
not determine the request ID, such as after a JSON parse error. The receiver
diagnoses such an uncorrelated error without replying to it or completing an
unrelated request. The connection remains usable, but this does not recover a
lost message or identify which pending request failed. A pending request still
needs a correlated response, cancellation, or connection teardown to complete.
This does not change the ID rules for normal requests, successful responses, or
`$/cancelRequest`, and does not promise recovery from malformed framing.

For notifications the `id` is omitted, to signify that the callee does not respond to that message.

An example of a notification:
Expand Down
28 changes: 27 additions & 1 deletion docs/mstest-runner-protocol/server-mode-1.0.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,15 @@
"const": "2.0"
},
"id": {
"$ref": "#/$defs/rpcId"
"description": "Null when the peer could not determine the request ID. Such errors cannot be correlated to a pending request.",
"oneOf": [
{
"$ref": "#/$defs/rpcId"
},
{
"type": "null"
}
]
},
"error": {
"type": "object",
Expand All @@ -749,6 +757,24 @@
"method"
]
},
"allOf": [
{
"if": {
"properties": {
"id": {
"type": "null"
}
}
},
"then": {
"not": {
"required": [
"result"
]
}
}
}
],
"additionalProperties": true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,12 @@ private void Dispatch(RpcMessage message, CancellationToken cancellationToken)

break;

case ErrorMessage error:
if (_pendingRequests.TryGetValue(GetRequestKey(error.Id, error.StringId), out PendingRequest? failed))
case ErrorMessage { Id: null } error:
_logger.SafeLog(MtpClientLogLevel.Debug, $"Received an uncorrelated JSON-RPC error (code {error.ErrorCode}): {error.Message}");
break;

case ErrorMessage { Id: { } errorId } error:
if (_pendingRequests.TryGetValue(GetRequestKey(errorId, error.StringId), out PendingRequest? failed))
{
failed.Completion.TrySetException(new MtpServerErrorException(error.ErrorCode, error.Message));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ private static void RegisterClientSerializersCore()
{
ValidateJsonRpcHeader(properties);

if (properties.ContainsKey(JsonRpcStrings.Error)
&& properties.TryGetValue(JsonRpcStrings.Id, out object? errorId)
&& errorId is null)
{
return Deserialize<ErrorMessage>(properties);
}

if (properties.TryGetValue(JsonRpcStrings.Method, out object? methodObj) && methodObj is not null)
{
string method = (string)methodObj;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ private async Task HandleMessagesAsync(CancellationToken cancellationToken)
return;
}

if (message is ErrorMessage { Id: null } uncorrelatedError)
{
QueueLog(LogLevel.Debug, $"Received an uncorrelated JSON-RPC error (code {uncorrelatedError.ErrorCode}): {uncorrelatedError.Message}");
continue;
}

// Signal that we have to handle this request
_requestCounter.AddCount();

Expand Down Expand Up @@ -82,11 +88,11 @@ private async Task HandleMessagesAsync(CancellationToken cancellationToken)
completion => completion.TrySetResult(response));
break;

case ErrorMessage error:
case ErrorMessage { Id: { } errorId } error:
RemoteInvocationException exception = new(error.ErrorCode, error.Message, error.Data);
CompleteRequest(
ref _serverToClientRequests,
GetRequestKey(error.Id, error.StringId),
GetRequestKey(errorId, error.StringId),
completion => completion.TrySetException(exception));
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#nullable enable
*REMOVED*Microsoft.Testing.Platform.ServerMode.ErrorMessage.Deconstruct(out int Id, out int ErrorCode, out string! Message, out object? Data) -> void
*REMOVED*Microsoft.Testing.Platform.ServerMode.ErrorMessage.ErrorMessage(int Id, int ErrorCode, string! Message, object? Data) -> void
*REMOVED*Microsoft.Testing.Platform.ServerMode.ErrorMessage.Id.get -> int
Microsoft.Testing.Platform.ServerMode.ErrorMessage.Deconstruct(out int? Id, out int ErrorCode, out string! Message, out object? Data) -> void
Microsoft.Testing.Platform.ServerMode.ErrorMessage.ErrorMessage(int? Id, int ErrorCode, string! Message, object? Data) -> void
Microsoft.Testing.Platform.ServerMode.ErrorMessage.Id.get -> int?
Microsoft.Testing.Platform.OutputDevice.GitHubActionsCommandOutputDeviceData
Microsoft.Testing.Platform.OutputDevice.GitHubActionsCommandOutputDeviceData.GitHubActionsCommandOutputDeviceData(string! text) -> void
Microsoft.Testing.Platform.OutputDevice.TerminalOutputDevice.TestSessionFailureProvider.get -> System.Func<bool>!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ private static void RegisterDefaultDeserializers(Dictionary<Type, JsonDeserializ
{
ValidateJsonRpcHeader(json, jsonElement);

if (jsonElement.TryGetProperty(JsonRpcStrings.Error, out _)
&& jsonElement.TryGetProperty(JsonRpcStrings.Id, out JsonElement errorId)
&& errorId.ValueKind == JsonValueKind.Null)
{
return json.Bind<ErrorMessage>(jsonElement);
}

if (json.TryBind(jsonElement, out string? method, JsonRpcStrings.Method))
{
bool hasId = TryGetRpcId(jsonElement, out int id, out string? stringId);
Expand Down Expand Up @@ -338,9 +345,41 @@ or JsonRpcMethods.TestingRunTests
{
ValidateJsonRpcHeader(json, jsonElement);

int id = BindRpcId(jsonElement, out string? stringId);
if (!jsonElement.TryGetProperty(JsonRpcStrings.Id, out JsonElement idElement))
{
throw new MessageFormatException($"'{JsonRpcStrings.Id}' field is missing");
}

string? stringId = null;
int? id = idElement.ValueKind == JsonValueKind.Null ? null : BindRpcId(jsonElement, out stringId);
JsonElement error = jsonElement.GetProperty(JsonRpcStrings.Error);

if (id is null)
{
if (jsonElement.TryGetProperty(JsonRpcStrings.Method, out _) || jsonElement.TryGetProperty(JsonRpcStrings.Result, out _))
{
throw new MessageFormatException("An uncorrelated error response cannot contain 'method' or 'result'");
}

if (error.ValueKind != JsonValueKind.Object)
{
throw new MessageFormatException("'error' field has wrong type (expected Object)");
}

if (!error.TryGetProperty(JsonRpcStrings.Code, out JsonElement codeElement)
|| codeElement.ValueKind != JsonValueKind.Number
|| !codeElement.TryGetInt32(out _))
{
throw new MessageFormatException("error.code field must be an integer");
}

if (!error.TryGetProperty(JsonRpcStrings.Message, out JsonElement messageElement)
|| messageElement.ValueKind != JsonValueKind.String)
{
throw new MessageFormatException("error.message field must be a string");
}
}

int code = json.Bind<int>(error, JsonRpcStrings.Code);
string message = json.Bind<string>(error, JsonRpcStrings.Message);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private static void RegisterDefaultSerializers(Dictionary<Type, JsonSerializer>
return
[
(JsonRpcStrings.JsonRpc, "2.0"),
(JsonRpcStrings.Id, error.StringId ?? (object)error.Id),
(JsonRpcStrings.Id, error.StringId ?? (object?)error.Id),
(JsonRpcStrings.Error, errorMsg)
];
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ internal sealed record NotificationMessage(string Method, object? Params) : RpcM

/// <summary>
/// An error message is sent if some exception was thrown when processing the request.
/// A null ID indicates that the peer could not determine the request ID.
/// </summary>
internal sealed record ErrorMessage(int Id, int ErrorCode, string Message, object? Data) : RpcMessage
internal sealed record ErrorMessage(int? Id, int ErrorCode, string Message, object? Data) : RpcMessage
{
public string? StringId { get; init; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ private static void RegisterDeserializers()
{
ValidateJsonRpcHeader(properties);

if (properties.ContainsKey(JsonRpcStrings.Error)
&& properties.TryGetValue(JsonRpcStrings.Id, out object? errorId)
&& errorId is null)
{
return Deserialize<ErrorMessage>(properties);
}

if (properties.TryGetValue(JsonRpcStrings.Method, out object? methodObj) && methodObj is not null)
{
string method = (string)methodObj;
Expand Down Expand Up @@ -290,7 +297,19 @@ or JsonRpcMethods.TestingRunTests
Deserializers[typeof(ErrorMessage)] = new ObjectDeserializer<ErrorMessage>(properties =>
{
ValidateJsonRpcHeader(properties);
object idObj = GetRequiredPropertyFromJson<object>(properties, JsonRpcStrings.Id);
if (!properties.TryGetValue(JsonRpcStrings.Id, out object? idObj))
{
throw new MessageFormatException($"'{JsonRpcStrings.Id}' field is missing");
}

int? id = idObj is null
? null
: GetIdFromJson(idObj) ?? throw new MessageFormatException("id field should be a string or an int");
if (id is null && (properties.ContainsKey(JsonRpcStrings.Method) || properties.ContainsKey(JsonRpcStrings.Result)))
{
throw new MessageFormatException("An uncorrelated error response cannot contain 'method' or 'result'");
}

IDictionary<string, object> errorObj = GetRequiredPropertyFromJson<IDictionary<string, object>>(properties, JsonRpcStrings.Error);

#if !NETCOREAPP
Expand All @@ -300,8 +319,6 @@ or JsonRpcMethods.TestingRunTests
errorObj[JsonRpcStrings.Data] = null!;
}
#endif
int id = GetIdFromJson(idObj) ?? throw new MessageFormatException("id field should be a string or an int");

if (!errorObj.TryGetValue(JsonRpcStrings.Code, out object? codeObj) ||
codeObj is not int code)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private static void RegisterRpcMessageSerializers()
Dictionary<string, object?> values = new()
{
[JsonRpcStrings.JsonRpc] = "2.0",
[JsonRpcStrings.Id] = error.StringId ?? (object)error.Id,
[JsonRpcStrings.Id] = error.StringId ?? (object?)error.Id,
[JsonRpcStrings.Error] = new Dictionary<string, object?>
{
[JsonRpcStrings.Code] = error.ErrorCode,
Expand Down
Loading
Loading