chitralverma opened a new issue, #9354:
URL: https://github.com/apache/opendal/issues/9354

   > Prepared with AI assistance (Claude Code, Claude Opus 5.5, effort xhigh). 
The behavior below was reproduced locally; all output is copied from real runs.
   
   ### OpenDAL version or commit
   
   `main` at commit `809b76472`
   
   ### Affected service or component
   
   bindings/nodejs: `Writer`, `Lister`, and `Operator`. Reproduced with the 
`webdav` and `http` services against a local HTTP server that only holds 
requests open. The bug is in the binding, not in a service.
   
   ### Describe the bug
   
   #9258 fixed this for `Reader`. napi-rs 3.16.3 frees a class instance when 
its worker's environment shuts down, even while a `#[napi] async fn(&self)` 
call on it is still pending (napi-rs/napi-rs#3748). The binding's other `async 
fn(&self)` methods have the same bug:
   
   - **`Writer`:** `write`, `close`, `abort`
   - **`Lister`:** `next`
   - **`Operator`:** all 19 async methods (`stat`, `check`, `exists`, 
`createDir`, `read`, `reader`, `write`, `writer`, `copy`, `rename`, `delete`, 
`remove`, `removeAll`, `list`, `lister`, `presignRead`, `presignWrite`, 
`presignStat`, `presignDelete`)
   
   Suppose a worker terminates with one of these calls pending while the main 
thread also uses OpenDAL. Then:
   
   - **`Writer.close()`:** the request stays open. When it ends, the call 
resumes on the freed `Writer` and the process crashes.
   - **`Lister.next()`:** freeing the `Lister` drops its request, but the 
pending call survives, and the process crashes with SIGSEGV at exit.
   - **`Operator.createDir()`:** the request stays open, and the process 
crashes with SIGSEGV once it ends.
   - **`Operator.read()` and `Operator.stat()`:** the request stays open after 
termination, and the call keeps running after its `Operator` is freed. No crash 
was observed.
   
   [`SECURITY-THREAT-MODEL.md` 
ยง4.7](https://github.com/apache/opendal/blob/main/SECURITY-THREAT-MODEL.md#47-ffi-and-language-bindings)
 lists binding use-after-free bugs as in scope, and 
[`SECURITY.md`](https://github.com/apache/opendal/blob/main/SECURITY.md) asks 
for private reports: should I also email [email protected]?
   
   ### Steps to Reproduce
   
   Environment: macOS 27.0.1 arm64, Node v20.20.2 and v24.16.0, rustc 1.96.0, 
napi 3.16.3, napi-derive 3.7.2.
   
   ```bash
   cd bindings/nodejs
   pnpm install --frozen-lockfile
   pnpm build   # release; pnpm build:debug for a debug build
   node teardown.mjs writer-close   # or lister-next, op-read, op-stat, 
op-createdir
   ```
   
   `teardown.mjs`, saved in `bindings/nodejs`:
   
   ```js
   import { once } from 'node:events'
   import { createServer } from 'node:http'
   import { setTimeout as sleep } from 'node:timers/promises'
   import { Worker } from 'node:worker_threads'
   
   const entry = new URL('./index.mjs', import.meta.url).href
   await import(entry) // The main thread uses OpenDAL too, so the async 
runtime outlives the worker.
   
   const CASES = {
     'writer-close': ['webdav', `const w = await op.writer('object'); await 
w.write('hello'); w.close()`],
     'lister-next': ['webdav', `const l = await op.lister('dir/'); l.next()`],
     'op-read': ['http', `op.read('object')`],
     'op-stat': ['http', `op.stat('object')`],
     'op-createdir': ['webdav', `op.createDir('dir/')`],
   }
   const [scheme, call] = CASES[process.argv[2]]
   
   const held = []
   const server = createServer((req, res) => held.push(res)) // Hold every 
request open.
   server.listen(0, '127.0.0.1')
   await once(server, 'listening')
   
   const worker = new Worker(
     `
       const { workerData } = require('node:worker_threads')
       import(workerData.entry).then(async ({ Operator }) => {
         const op = new Operator(workerData.scheme, { endpoint: 
workerData.endpoint })
         ${call}
       })
     `,
     { eval: true, workerData: { entry, scheme, endpoint: 
`http://127.0.0.1:${server.address().port}` } },
   )
   while (held.length === 0) await sleep(10)
   const released = once(held[0], 'close').then(() => 'released')
   console.log('terminate ->', await worker.terminate())
   console.log('request 1 s later:', await Promise.race([released, 
sleep(1000).then(() => 'still open')]))
   server.closeAllConnections() // End the held request.
   server.close()
   await sleep(500)
   console.log('done')
   ```
   
   ### Actual Behavior
   
   I ran 10 runs per case and configuration:
   
   | Case | Request 1 s after `terminate()` | Exit, debug build | Exit, release 
build |
   | --- | --- | --- | --- |
   | `writer-close` | still open in 10/10 | abort (134) in 10/10 | SIGSEGV 
(139) in 10/10 |
   | `lister-next` | released in 10/10 | SIGSEGV (139) in 10/10 | SIGSEGV (139) 
in 10/10 |
   | `op-read` | still open in 10/10 | 0 in 10/10 | 0 in 10/10 |
   | `op-stat` | still open in 10/10 | 0 in 10/10 | 0 in 10/10 |
   | `op-createdir` | still open in 10/10 | SIGSEGV (139) in 10/10 | SIGSEGV 
(139) in 10/10 |
   
   Results were the same on Node v20.20.2 and v24.16.0.
   
   The debug abort for `writer-close` (excerpt):
   
   ```
   terminate -> 1
   request 1 s later: still open
   
   thread 'tokio-rt-worker' panicked at 
.../library/core/src/slice/iter.rs:142:1:
   unsafe precondition(s) violated: slice::from_raw_parts requires the pointer 
to be aligned and non-null, and the total size of the slice not to exceed 
`isize::MAX`
   thread caused non-unwinding panic. aborting.
   ```
   
   On Linux (Ubuntu 24.04 arm64 container, Node v20.20.2, debug build), the 
regression tests for the planned PRs showed the same open requests for 
`Writer.close()`, `Operator.read()`, and `Operator.stat()`. The `Lister` crash 
appeared in only 1 of 18 runs there.
   
   ### Expected Behavior
   
   Terminating a worker does not crash the process, and its pending calls 
release their requests, as `Reader` calls do since #9258.
   
   ### Additional Context
   
   I plan three draft PRs that apply the #9258 pattern. They do not depend on 
each other and merge cleanly in any order:
   
   - **`Writer`:** `Writer` methods and `Operator.writer`.
   - **`Lister`:** `Lister.next` and `Operator.lister`.
   - **`Operator`:** the other 17 `Operator` methods.
   
   `Operator.writer` and `Operator.lister` go with their classes because they 
construct them.
   
   Like #9258, the PRs cancel a pending call when its worker's environment 
shuts down. On `main`, most mutating `Operator` calls kept running after 
termination. Cancelling one, such as `removeAll` or a multipart `write`, can 
leave partial results. The PRs call this out for review.
   
   ### Reproduction confirmation
   
   - [x] I reproduced this issue against the affected component. For a 
service-specific report, I used the actual service rather than only a mock, 
emulator, or source-code analysis.
   
   ### Are you willing to submit a PR to fix this bug?
   
   - [x] Yes, I would like to submit a PR.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to