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: don't close while writing #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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