Skip to content

Commit 14ab2ed

Browse files
committed
fix(ios): bound split-message headers and flush a superseded message
The piece header is matched against any entry the NativeScript framework logs, app output included, and its count sized an array up front. A line such as "[ns 1 1/9999999999] x" made that allocation throw and crash the CLI, and a count in the tens of millions stalled the event loop. Headers now only match the digit counts the runtime writes (ids of up to 8 hex digits, counts of up to 7 decimal ones), anything longer passes through as an ordinary message, and pieces are kept by index so a large count costs nothing until pieces actually arrive. When the same id showed up again with a different count, the pending message was replaced but its timer kept running: it later dropped the new message's pieces and printed the old one. The old message is now printed straight away, with its missing pieces marked as lost, and its timer is cleared.
1 parent 83f186d commit 14ab2ed

2 files changed

Lines changed: 82 additions & 28 deletions

File tree

‎lib/common/mobile/ios/ios-log-entry.ts‎

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,11 @@ export const IOS_LOG_FORMAT = 1;
8484
// "[ns <id> <index>/<count>] <piece>". The id is unique per message in the
8585
// process, and the pieces are consecutive slices of the message. A piece of
8686
// nothing but whitespace arrives as the bare header: the unified log strips
87-
// whitespace from the end of every entry.
88-
export const IOS_LOG_PIECE_HEADER = /^\[ns ([0-9a-f]+) (\d+)\/(\d+)\](?: |$)/;
87+
// whitespace from the end of every entry. The runtime writes ids of at most 8
88+
// hex digits and counts of at most 7 decimal ones, so anything longer is text
89+
// that only looks like a header.
90+
export const IOS_LOG_PIECE_HEADER =
91+
/^\[ns ([0-9a-f]{1,8}) (\d{1,7})\/(\d{1,7})\](?: |$)/;
8992

9093
// Where a piece that never arrived belongs; os_log marks truncated text alike.
9194
const LOST_PIECE = "<…lost…>";
@@ -97,8 +100,9 @@ const PIECE_WAIT_MS = 500;
97100

98101
interface IPendingLogMessage {
99102
entry: Mobile.IiOSLogEntry;
100-
pieces: string[];
101-
received: number;
103+
count: number;
104+
// By index; the count comes from the log, so it is never preallocated.
105+
pieces: Map<number, string>;
102106
timer: NodeJS.Timeout;
103107
}
104108

@@ -127,48 +131,59 @@ export class IOSLogChunkStitcher {
127131

128132
const key = `${source}:${header[1]}`;
129133
let message = this.pending.get(key);
130-
if (!message || message.pieces.length !== count) {
131-
message = {
132-
entry,
133-
pieces: new Array(count),
134-
received: 0,
135-
timer: null,
136-
};
134+
if (message && message.count !== count) {
135+
this.finish(key, message, emit);
136+
message = undefined;
137+
}
138+
if (!message) {
139+
message = { entry, count, pieces: new Map(), timer: null };
137140
this.pending.set(key, message);
138141
}
139-
if (message.pieces[index] === undefined) {
140-
message.pieces[index] = entry.message.slice(header[0].length);
141-
message.received++;
142+
if (!message.pieces.has(index)) {
143+
message.pieces.set(index, entry.message.slice(header[0].length));
142144
}
143145
if (index === 0) {
144146
message.entry = entry;
145147
}
146148

147149
clearTimeout(message.timer);
148-
const finish = () => {
149-
this.pending.delete(key);
150-
emit({ ...message.entry, message: joinPieces(message) });
151-
};
152-
if (message.received === count) {
153-
finish();
150+
if (message.pieces.size === count) {
151+
this.finish(key, message, emit);
154152
return;
155153
}
156154

157-
message.timer = setTimeout(finish, PIECE_WAIT_MS);
155+
const pending = message;
156+
message.timer = setTimeout(
157+
() => this.finish(key, pending, emit),
158+
PIECE_WAIT_MS,
159+
);
158160
message.timer.unref();
159161
}
162+
163+
private finish(
164+
key: string,
165+
message: IPendingLogMessage,
166+
emit: (entry: Mobile.IiOSLogEntry) => void,
167+
): void {
168+
clearTimeout(message.timer);
169+
this.pending.delete(key);
170+
emit({ ...message.entry, message: joinPieces(message) });
171+
}
160172
}
161173

162174
function joinPieces(message: IPendingLogMessage): string {
163175
let text = "";
164-
for (let i = 0; i < message.pieces.length; i++) {
165-
if (message.pieces[i] === undefined) {
166-
if (!text.endsWith(LOST_PIECE)) {
167-
text += LOST_PIECE;
168-
}
169-
continue;
176+
let next = 0;
177+
const indexes = [...message.pieces.keys()].sort((a, b) => a - b);
178+
for (const index of indexes) {
179+
if (index > next) {
180+
text += LOST_PIECE;
170181
}
171-
text += message.pieces[i];
182+
text += message.pieces.get(index);
183+
next = index + 1;
184+
}
185+
if (next < message.count) {
186+
text += LOST_PIECE;
172187
}
173188
return text;
174189
}

‎lib/common/test/unit-tests/mobile/ios-log-entry.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,48 @@ describe("IOSLogChunkStitcher", () => {
225225
it("passes through text that only looks like a header", () => {
226226
add("[ns 8 3/2] CONSOLE LOG: x");
227227
add("[ns zz 1/2] CONSOLE LOG: y");
228+
add("[ns 8 0/2] CONSOLE LOG: z");
228229
assert.deepEqual(messages, [
229230
"[ns 8 3/2] CONSOLE LOG: x",
230231
"[ns zz 1/2] CONSOLE LOG: y",
232+
"[ns 8 0/2] CONSOLE LOG: z",
233+
]);
234+
});
235+
236+
it("passes through headers with more digits than the runtime writes", () => {
237+
add("[ns 1 1/9999999999] x");
238+
add("[ns 1 1/12345678] x");
239+
add("[ns 12345678 1/12345678] x");
240+
add("[ns 123456789 1/2] x");
241+
assert.deepEqual(messages, [
242+
"[ns 1 1/9999999999] x",
243+
"[ns 1 1/12345678] x",
244+
"[ns 12345678 1/12345678] x",
245+
"[ns 123456789 1/2] x",
246+
]);
247+
});
248+
249+
it("handles the largest count the runtime writes", () => {
250+
add("[ns ffffffff 9999999/9999999] last");
251+
assert.isEmpty(messages);
252+
clock.tick(500);
253+
assert.deepEqual(messages, ["<…lost…>last"]);
254+
});
255+
256+
it("prints a message at once when its id starts over with another count", () => {
257+
add("[ns 3 1/2] CONSOLE LOG: old");
258+
clock.tick(300);
259+
add("[ns 3 1/3] CONSOLE LOG: new");
260+
assert.deepEqual(messages, ["CONSOLE LOG: old<…lost…>"]);
261+
262+
clock.tick(300);
263+
add("[ns 3 2/3] b");
264+
clock.tick(100);
265+
add("[ns 3 3/3] c");
266+
clock.tick(1000);
267+
assert.deepEqual(messages, [
268+
"CONSOLE LOG: old<…lost…>",
269+
"CONSOLE LOG: newbc",
231270
]);
232271
});
233272
});

0 commit comments

Comments
 (0)