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

actualClose can close fd while writing #98

Open
ronag opened this issue Aug 26, 2021 · 7 comments · May be fixed by #103
Open

actualClose can close fd while writing #98

ronag opened this issue Aug 26, 2021 · 7 comments · May be fixed by #103

Comments

@ronag
Copy link
Contributor

ronag commented Aug 26, 2021

actualClose can close fd while writing causing undefined behavior.

@ronag
Copy link
Contributor Author

ronag commented Aug 26, 2021

I think actualClose just needs a:

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

@mcollina
Copy link
Member

Did you see this happening?

@ronag
Copy link
Contributor Author

ronag commented Aug 26, 2021

Did you see this happening?

Nope. Just based on review of the code. These kind of undefined behaviors are very to actually notice in practice.

@mcollina
Copy link
Member

This is protected and it can't happen.

@ronag
Copy link
Contributor Author

ronag commented Aug 26, 2021

I'm not sure I agree. It only waits for it to be ready not to finish pending async writes (writing).

@ronag
Copy link
Contributor Author

ronag commented Aug 26, 2021

If you add the following assertion then the test suite will fail:

index 45bf86d..2d4f8a2 100644
--- a/index.js
+++ b/index.js
@@ -8,6 +8,7 @@ const path = require('path')
 const BUSY_WRITE_TIMEOUT = 100
 
 const sleep = require('atomic-sleep')
+const assert = require('assert')
 
 // 16 MB - magic number
 // This constant ensures that SonicBoom only needs
@@ -368,6 +369,7 @@ function actualClose (sonic) {
     sonic.once('ready', 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) {

@mcollina mcollina reopened this Aug 26, 2021
@mcollina
Copy link
Member

ah sorry! I ready 'drain' for 'ready'.

In practice I have not seen this being a problem - we have done extensive testing and we did not get corrupt data.

ronag added a commit to ronag/sonic-boom that referenced this issue Aug 29, 2021
@ronag ronag linked a pull request Aug 29, 2021 that will close this issue
ronag added a commit to ronag/sonic-boom that referenced this issue Aug 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants