Skip to content

Commit

Permalink
fix: don't close while writing
Browse files Browse the repository at this point in the history
Fixes: pinojs#98
  • Loading branch information
ronag committed Aug 29, 2021
1 parent ad88efd commit 0419e13
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const EventEmitter = require('events')
const inherits = require('util').inherits
const path = require('path')
const sleep = require('atomic-sleep')
const assert = require('assert')

const BUSY_WRITE_TIMEOUT = 100

Expand Down Expand Up @@ -374,6 +375,15 @@ function actualClose (sonic) {
sonic.once('ready', actualClose.bind(null, sonic))
return
}

sonic.destroyed = true

if (sonic._writing) {
sonic.once('drain', actualClose.bind(null, sonic))
return
}

assert(!sonic._writing)
// TODO write a test to check if we are not leaking fds
fs.close(sonic.fd, (err) => {
if (err) {
Expand All @@ -386,7 +396,6 @@ function actualClose (sonic) {
}
sonic.emit('close')
})
sonic.destroyed = true
sonic._bufs = []
}

Expand Down

0 comments on commit 0419e13

Please sign in to comment.