Scripted-bot softlock root cause: client-stocked SELECT_SKILL_URI /
SLIDE_OBJECT_URI hand emits (e.g. target selection on unit play / leader
attack) arrive as SIO BinaryEvent("hand", ...) with an ack-id. Our
DispatchSocketIo only had cases for "msg" and "alive" — "hand" fell to
the default Debug-drop with no SIO ack going back. Client's
stockEmitMessageMgr (RealTimeNetworkAgent.cs:1463) blocks subsequent
emits until the previous one is acked, so all follow-up PlayActions /
TurnEndActions / TurnEnd frames were stocked but never transmitted. The
loader hooks at EmitMsg (intent) not the socket layer, which is why
battle-traffic.ndjson shows the frames as sent while the server never
received them. ~10s later the client gives up and aborts the WS.
Wire-shape proof from data_dumps/captures/logs/websocket_output.txt:
line 619: [sio-in] uri=TurnStart pubSeq=17 ackId=16 ... (T3 start)
line 689: [ws-rx-text] preview=451-26["hand", {...}] ← unhandled
line 691: [ws-rx-bin] binLen=58 pendingFrame=hand
(no further [sio-in] entries — server received nothing else)
line 709: [ws-recv-exit] reason=OperationCanceled wsState=Aborted
New HandleHandEventAsync (RealParticipant.cs):
- Fire-and-forget hand frames (no ack-id; TOUCH_URI / SELECT_OBJECT_URI /
TURN_END_READY_URI) are silently swallowed — no queue-blocking risk
- Stocked hand frames decode the binary attachment via the same
msgpack-string + NodeCrypto.Decrypt pipeline as HandleMsgEventAsync,
parse the JSON, extract top-level "pubSeq", and SendSioAckAsync with
that pubSeq as the ack arg (matches what stockEmitMessageMgr.GetSelectData
expects to look up)
- Body shape is {"StockHandData":[uri_int, viewerId, udid, ...params,
pubSeq], "try":0, "pubSeq":N} — NOT a MsgEnvelope (no top-level "uri"),
so we can't reuse HandleMsgEventAsync as-is
- Missing-pubSeq fallback acks with arg=0 (rare path, logged at Warning)
so we never softlock from a malformed body
WireConstants gets the HandEvent = "hand" constant for the dispatch case.
In scripted/Bot mode the ack-only handler is correct (no opponent to
forward touches to). PvP-side forwarding semantics are unverified — see
docs/audits/battle-node-sio-events-2026-06-02.md (outer repo) for the
full event inventory and remaining gaps.
Tests:
- RealParticipantHandEventTests covers the three paths: stocked-with-ack,
fire-and-forget (no ack expected), missing-pubSeq fallback (arg=0). Each
drives a real hand frame through RunAsync via TestWebSocket and asserts
the SIO ack frame shape (43<ackId>[<arg>]) in outbound sends.
- 175 battle-node tests passing (was 172; +3 new). Full suite green.
Diagnostic logs ([sio-in] / [sio-out] / [ws-rx-text] / [ws-rx-bin] /
[ws-recv-exit] / [ws-loop-exit]) are left in place for one verification
cycle. After a live re-run confirms the fix, they should be stripped per
the audit doc's recommended-order step 2.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
147 lines
6.8 KiB
C#
147 lines
6.8 KiB
C#
using System.Net.WebSockets;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using MessagePack;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using NUnit.Framework;
|
|
using SVSim.BattleNode.Bridge;
|
|
using SVSim.BattleNode.Protocol;
|
|
using SVSim.BattleNode.Sessions;
|
|
using SVSim.BattleNode.Sessions.Participants;
|
|
using SVSim.BattleNode.Wire;
|
|
using SVSim.UnitTests.BattleNode.Infrastructure;
|
|
|
|
namespace SVSim.UnitTests.BattleNode.Sessions.Participants;
|
|
|
|
/// <summary>
|
|
/// Regression tests for the <c>"hand"</c> SIO event handler. The active bug at
|
|
/// <c>docs/audits/battle-node-sio-events-2026-06-02.md</c> §"Active bug" was:
|
|
/// client-stocked <c>SELECT_SKILL_URI</c> / <c>SLIDE_OBJECT_URI</c> hand emits arrive
|
|
/// with an ack-id; without a server ack, the client's <c>stockEmitMessageMgr</c> deadlocks
|
|
/// behind them and every subsequent emit (PlayActions, TurnEndActions, TurnEnd) is queued
|
|
/// but never transmitted. These tests drive a hand frame through the WS read loop and
|
|
/// inspect the outbound text-frame queue for the SIO ack.
|
|
/// </summary>
|
|
[TestFixture]
|
|
public class RealParticipantHandEventTests
|
|
{
|
|
// Any 32-char ASCII string is a valid AES key for NodeCrypto — DecryptForNode reads the
|
|
// key from the first 32 chars of the encrypted blob.
|
|
private const string TestKey = "abcdefghijklmnopqrstuvwxyz012345";
|
|
|
|
[Test]
|
|
public async Task Stocked_hand_event_acks_with_body_pubSeq()
|
|
{
|
|
var ws = new TestWebSocket();
|
|
var p = new RealParticipant(ws, viewerId: 906_243_102L, FixtureCtx(),
|
|
NullLogger<RealParticipant>.Instance);
|
|
|
|
// Client-shape hand body for a SELECT_SKILL emit: StockHandData[0] = uri_int
|
|
// (HAND_URI_TYPE.SELECT_SKILL_URI = 2), top-level "try" + "pubSeq" added by
|
|
// EmitMsgUriPack. The handler reads top-level "pubSeq" — body contents otherwise
|
|
// don't matter for ack semantics.
|
|
const long expectedPubSeq = 42L;
|
|
var body = $"{{\"StockHandData\":[2,906243102,\"u\",1,0,{expectedPubSeq}],\"try\":0,\"pubSeq\":{expectedPubSeq}}}";
|
|
EnqueueHandFrame(ws, ackId: 26, body: body);
|
|
ws.CompleteIncoming();
|
|
|
|
await p.RunAsync(CancellationToken.None);
|
|
|
|
var ackFrame = FindAckFrame(ws, ackId: 26);
|
|
Assert.That(ackFrame, Is.Not.Null,
|
|
$"Expected an SIO Ack frame for ackId=26 in outbound sends; got: [{string.Join(", ", AllTextSends(ws))}]");
|
|
Assert.That(ackFrame, Does.Contain($"[{expectedPubSeq}]"),
|
|
"Ack arg must echo the body's pubSeq so the client's stockEmitMessageMgr.GetSelectData lookup succeeds.");
|
|
}
|
|
|
|
[Test]
|
|
public async Task Hand_event_without_ackId_is_swallowed_silently_no_ack_sent()
|
|
{
|
|
// Fire-and-forget hand emits (TOUCH_URI, SELECT_OBJECT_URI, TURN_END_READY_URI) arrive
|
|
// without an ack-id and don't block the client's emit queue. We should swallow them
|
|
// without trying to decode or ack.
|
|
var ws = new TestWebSocket();
|
|
var p = new RealParticipant(ws, viewerId: 1, FixtureCtx(),
|
|
NullLogger<RealParticipant>.Instance);
|
|
|
|
var body = "{\"StockHandData\":[1,1,\"u\",0],\"try\":0}";
|
|
EnqueueHandFrame(ws, ackId: null, body: body);
|
|
ws.CompleteIncoming();
|
|
|
|
await p.RunAsync(CancellationToken.None);
|
|
|
|
// Only the EIO Open handshake should be in Sends; no Ack frame.
|
|
var ackFrames = AllTextSends(ws).Where(s => s.StartsWith("43")).ToList();
|
|
Assert.That(ackFrames, Is.Empty,
|
|
$"No-ack-id hand frame must not produce an Ack; got: [{string.Join(", ", ackFrames)}]");
|
|
}
|
|
|
|
[Test]
|
|
public async Task Hand_event_with_missing_pubSeq_falls_back_to_ack_arg_0()
|
|
{
|
|
// If a stocked hand frame ever arrives without a pubSeq, we still ack so the
|
|
// client doesn't softlock — but with arg=0 (the client's GetSelectData lookup
|
|
// will miss and OnAck fires with null selectData, which is the same path as a
|
|
// normal cache-miss; not great, but not a deadlock).
|
|
var ws = new TestWebSocket();
|
|
var p = new RealParticipant(ws, viewerId: 1, FixtureCtx(),
|
|
NullLogger<RealParticipant>.Instance);
|
|
|
|
var body = "{\"StockHandData\":[2,1,\"u\"],\"try\":0}"; // no pubSeq
|
|
EnqueueHandFrame(ws, ackId: 99, body: body);
|
|
ws.CompleteIncoming();
|
|
|
|
await p.RunAsync(CancellationToken.None);
|
|
|
|
var ackFrame = FindAckFrame(ws, ackId: 99);
|
|
Assert.That(ackFrame, Is.Not.Null,
|
|
"Missing-pubSeq fallback should still ack (arg=0), not silently swallow.");
|
|
Assert.That(ackFrame, Does.Contain("[0]"),
|
|
"Fallback ack arg should be 0.");
|
|
}
|
|
|
|
// -----------------------------------------------------------------------
|
|
// Helpers
|
|
// -----------------------------------------------------------------------
|
|
|
|
/// <summary>
|
|
/// Enqueue an SIO BinaryEvent("hand", {placeholder}) text frame followed by its single
|
|
/// binary attachment (NodeCrypto-encrypted msgpack-string of <paramref name="body"/>).
|
|
/// Wire format mirrors what <c>EmitFrontStockData:717-720</c> emits.
|
|
/// </summary>
|
|
private static void EnqueueHandFrame(TestWebSocket ws, int? ackId, string body)
|
|
{
|
|
// Text frame: 4 (EIO Message) + 5 (SIO BinaryEvent) + 1- (1 attachment) + ackId + json
|
|
var ackPart = ackId.HasValue ? ackId.Value.ToString() : "";
|
|
var text = $"451-{ackPart}[\"hand\",{{\"_placeholder\":true,\"num\":0}}]";
|
|
ws.EnqueueIncoming(Encoding.UTF8.GetBytes(text), WebSocketMessageType.Text);
|
|
|
|
// Binary attachment: EIO Message prefix (0x04) + msgpack-string(NodeCrypto.Encrypt(json)).
|
|
var encrypted = NodeCrypto.EncryptForNode(body, TestKey);
|
|
var msgpackBytes = MessagePackSerializer.Serialize(encrypted);
|
|
var prefixed = new byte[msgpackBytes.Length + 1];
|
|
prefixed[0] = (byte)EngineIoPacketType.Message;
|
|
Buffer.BlockCopy(msgpackBytes, 0, prefixed, 1, msgpackBytes.Length);
|
|
ws.EnqueueIncoming(prefixed, WebSocketMessageType.Binary);
|
|
}
|
|
|
|
private static IEnumerable<string> AllTextSends(TestWebSocket ws) =>
|
|
ws.Sends
|
|
.Where(f => f.Type == WebSocketMessageType.Text)
|
|
.Select(f => Encoding.UTF8.GetString(f.Payload));
|
|
|
|
/// <summary>
|
|
/// SIO Ack wire form: EIO Message (4) + SIO Ack (3) + ackId + [arg]. Filter the text
|
|
/// sends for that shape.
|
|
/// </summary>
|
|
private static string? FindAckFrame(TestWebSocket ws, int ackId) =>
|
|
AllTextSends(ws).FirstOrDefault(s => s.StartsWith($"43{ackId}["));
|
|
|
|
private static MatchContext FixtureCtx() => new(
|
|
SelfDeckCardIds: Enumerable.Range(1, 30).Select(_ => 100_011_010L).ToList(),
|
|
ClassId: "1", CharaId: "1", CardMasterName: "card_master_node_10015",
|
|
CountryCode: "KOR", UserName: "Player", SleeveId: "3000011",
|
|
EmblemId: "701441011", DegreeId: "300003", FieldId: 43, IsOfficial: 0,
|
|
BattleType: 11);
|
|
}
|