fix(devices): USB truncation mode 2 — close() kills the in-flight usblp URB
Build desktop / desktop (push) Successful in 4m16s
CI / check (push) Successful in 43s
Build & push images / images (push) Successful in 2m51s

The chunked-write fix (81bc2e3) still truncated on hardware: the lab
test slip stopped mid-sentence with no feed and no cut (text hidden
until the feed button). Verified against drivers/usb/class/usblp.c:

- write() returns at URB SUBMISSION, not completion;
- only ONE write URB is in flight (the next write EAGAINs until it
  completes);
- usblp_release() — our close() — KILLS in-flight URBs.

The printer drains bulk data at PRINT speed (tiny internal buffer on
these clones), so closing right after the last accepted write cancels
the still-transferring tail — exactly where the feed + GS V cut bytes
live. Kernel-accepted ≠ printer-received.

Fix: the one-URB rule makes acceptance of write N a completion
certificate for write N−1. writeAllUsb now writes the payload's FINAL
BYTE alone — its acceptance proves everything before it is physically
in the printer — then drains 300 ms for that single packet before the
caller closes. New test pins the final-byte-alone chunking; wiki
printer-usb-transport.md carries the kernel-level account.

Claude-Session: https://claude.ai/code/session_01Xcm6ikLgGoCxxHrxtjkk5V
This commit is contained in:
2026-07-07 10:50:47 +02:00
parent 6f3f6ca596
commit 011fe5a4c4
4 changed files with 80 additions and 20 deletions
@@ -185,11 +185,12 @@ describe("stamp (Albanian date format)", () => {
});
});
describe("writeAllUsb — the char device's partial writes / EAGAIN (field bug 2026-07-06)", () => {
// A NONBLOCK usblp fd accepts only what fits the printer's USB buffer per write.
// The old single-write path dropped everything past the first buffer — the ICS
// XP-K200L printed the ticket's text head but lost the barcode and the cut. A
// regular file can't reproduce that, so these drive the loop with a fake handle.
describe("writeAllUsb — partial writes / EAGAIN / close-cancel (field bugs 2026-07-06/07)", () => {
// A NONBLOCK usblp fd accepts only what fits the printer's USB buffer per write,
// write() returns at URB submission, and close() KILLS the in-flight URB — so the
// loop must deliver every byte AND certify delivery before the caller may close
// (final byte written alone; its acceptance proves all prior bytes landed). A
// regular file can't reproduce any of that, so these drive a fake handle.
/** Accepts at most `cap` bytes per call; records everything accepted in order. */
function slowHandle(cap: number) {
@@ -207,10 +208,18 @@ describe("writeAllUsb — the char device's partial writes / EAGAIN (field bug 2
it("delivers the WHOLE payload across many short writes (barcode + cut included)", async () => {
const payload = renderTicket({ ticketId: "12345678901", issuedAt: "2026-06-21T10:00:00.000Z" });
const h = slowHandle(100); // way smaller than the job → many partial writes
await writeAllUsb(h, payload, Date.now() + 2000);
await writeAllUsb(h, payload, Date.now() + 2000, 5);
expect(Buffer.concat(h.chunks).equals(payload)).toBe(true);
});
it("the FINAL byte is written alone — the delivery certificate before close", async () => {
const payload = Buffer.from("x".repeat(5000)); // > one 4K chunk
const h = slowHandle(100_000); // accepts anything → chunking is ours, not the cap's
await writeAllUsb(h, payload, Date.now() + 2000, 5);
expect(Buffer.concat(h.chunks).equals(payload)).toBe(true);
expect(h.chunks.at(-1)!.length).toBe(1); // usblp: its acceptance proves the rest landed
});
it("retries EAGAIN (buffer full) until the kernel accepts the rest", async () => {
const payload = Buffer.from("x".repeat(300));
let calls = 0;
@@ -228,7 +237,7 @@ describe("writeAllUsb — the char device's partial writes / EAGAIN (field bug 2
return Promise.resolve({ bytesWritten: n });
},
};
await writeAllUsb(h, payload, Date.now() + 2000);
await writeAllUsb(h, payload, Date.now() + 2000, 5);
expect(Buffer.concat(accepted).equals(payload)).toBe(true);
});