Skip to content
This repository has been archived by the owner on Aug 7, 2019. It is now read-only.

Fix errors caused by overlapping reads #8

Merged
merged 2 commits into from
Jan 15, 2019
Merged

Conversation

majorhayes
Copy link
Collaborator

Issue

Since transferIn is asynchronous, it's possible to have multiple async loops (see webUsbDevice.read()) to call transferIn, causing some packets to end up in the wrong read loop.

This manifests by a look expecting the first packet to have a header with a messageLength, but since the packet it receives is not really a first packet, but the middle of some other read, the read length is incorrect and causes an error when attempting to create a massively large array.

Resolution

Use p-queue to add an async queue with a concurrency of 1 to only allow one message and response to the device at a time.

Extra

This PR also simplifies some code around reading message length and concatenating packets.

for (let i = 0; i < devicesToInitialize.length; i++) {
const usbDevice = devicesToInitialize[i]

const devicesToInitialize = devices || (await window.navigator.usb.getDevices())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

src/webUSBDevice.ts Outdated Show resolved Hide resolved
* manager: Don't add devices that are already managed
* webUsbDevice: Simplify loop to join packets
for (let k = 0; k < first.byteLength; k++) {
buffer[k] = first.getUint8(k)
}
const valid = first.getUint32(0) === 1059267328
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind writing this constant it hex? I think that'll clarify what it's for.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just saw this after the merge (you didn't do Request Changes). There is a comment directly above this, but I can update the comment and the constant.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants