Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix out of memory crush, when there are no data to read on unix systems #175

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 30 additions & 26 deletions lib/unix-read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,39 @@ export const unixRead = async ({
throw new BindingsError('Port is not open', { canceled: true })
}

try {
const { bytesRead } = await fsReadAsync(binding.fd, buffer, offset, length, null)
if (bytesRead === 0) {
return unixRead({ binding, buffer, offset, length, fsReadAsync })
}
logger('Finished read', bytesRead, 'bytes')
return { bytesRead, buffer }
} catch (err) {
logger('read error', err)
if (err.code === 'EAGAIN' || err.code === 'EWOULDBLOCK' || err.code === 'EINTR') {
if (!binding.isOpen) {
throw new BindingsError('Port is not open', { canceled: true })
// eslint-disable-next-line no-constant-condition
while (true) {
try {
const {bytesRead} = await fsReadAsync(binding.fd, buffer, offset, length, null)
if (bytesRead === 0) {
await new Promise((resolve) => setTimeout(resolve, 10))
continue
}
logger('Finished read', bytesRead, 'bytes')
return {bytesRead, buffer}
} catch (err) {
logger('read error', err)
if (err.code === 'EAGAIN' || err.code === 'EWOULDBLOCK' || err.code === 'EINTR') {
if (!binding.isOpen) {
throw new BindingsError('Port is not open', {canceled: true})
}
logger('waiting for readable because of code:', err.code)
await readable(binding)
continue
}
logger('waiting for readable because of code:', err.code)
await readable(binding)
return unixRead({ binding, buffer, offset, length, fsReadAsync })
}

const disconnectError =
err.code === 'EBADF' || // Bad file number means we got closed
err.code === 'ENXIO' || // No such device or address probably usb disconnect
err.code === 'UNKNOWN' ||
err.errno === -1 // generic error
const disconnectError =
err.code === 'EBADF' || // Bad file number means we got closed
err.code === 'ENXIO' || // No such device or address probably usb disconnect
err.code === 'UNKNOWN' ||
err.errno === -1 // generic error

if (disconnectError) {
err.disconnect = true
logger('disconnecting', err)
}
if (disconnectError) {
err.disconnect = true
logger('disconnecting', err)
}

throw err
throw err
}
}
}