Skip to content

Commit bfc1b5d

Browse files
committed
Fix "TypeError: Cannot read properties of undefined (reading 'length')" if unlucky with timing
https://github.com/orgs/espruino/discussions/7868
1 parent 8dfaba0 commit bfc1b5d

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

core/serial.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,13 @@ To add a new serial device, you must add an object to
241241
setTimeout(writeChunk, 50);
242242
return;
243243
}
244-
var chunk;
245-
if (!connection.txDataQueue.length) {
244+
if (!connection.txDataQueue.length) { // we're finished!
245+
connection.txInProgress = false;
246246
connection.updateProgress();
247247
return;
248248
}
249-
var txItem = connection.txDataQueue[0];
249+
connection.txInProgress = true;
250+
var chunk, txItem = connection.txDataQueue[0];
250251
connection.updateProgress(txItem.maxLength - (txItem.data?txItem.data.length:0), txItem.maxLength);
251252
if (txItem.data.length <= connection.chunkSize) {
252253
chunk = txItem.data;
@@ -255,7 +256,6 @@ To add a new serial device, you must add an object to
255256
chunk = txItem.data.substr(0,connection.chunkSize);
256257
txItem.data = txItem.data.substr(connection.chunkSize);
257258
}
258-
connection.txInProgress = true;
259259
log(2, "Sending "+ JSON.stringify(chunk));
260260
connection.writeLowLevel(chunk).then(function() {
261261
log(3, "Sent");
@@ -269,7 +269,6 @@ To add a new serial device, you must add an object to
269269
}
270270
if (!(promise instanceof Promise))
271271
promise = Promise.resolve();
272-
connection.txInProgress = false;
273272
promise.then(writeChunk); // if txItem.callback() returned a promise, wait until it completes before continuing
274273
}, function(error) {
275274
log(1, 'SEND ERROR: ' + error);

0 commit comments

Comments
 (0)