fix(ext/napi): don't close handle scopes in NAPI as the pointers are invalid (#21629)

`napi_open_handle_scope` was returning a bogus handle_scope and we were
trying to close it in `napi_close_handle_scope`.

This is a bit of a challenge to test, but the following testcase comes
from #21601 and appears to be fixed by this.

```
import { decode } from "https://deno.land/std@0.209.0/encoding/base64.ts";
import sharp from "npm:sharp";

const img = 'iVBORw0KGgoAAAANSUhEUgAAAQAAAAEAAQMAAABmvDolAAAAA1BMVEWq09/P7Lz1AAAAH0lEQVRoge3BAQ0AAADCoPdPbQ43oAAAAAAAAAAAvg0hAAABmmDh1QAAAABJRU5ErkJggg==';
Deno.test("async", async () => {
  const id = setTimeout(() => Deno.exit(1), 1000);
  await sharp(decode(img)).toBuffer();
  await sharp(decode(img)).toBuffer();
  clearTimeout(id);
});
```
This commit is contained in:
Matt Mastracci 2023-12-18 08:48:52 -07:00 committed by GitHub
parent 6b482d7392
commit d51fda9e14
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1361,13 +1361,15 @@ fn napi_close_escapable_handle_scope(
#[napi_sym::napi_sym]
fn napi_close_handle_scope(
env: *mut Env,
scope: napi_handle_scope,
_scope: napi_handle_scope,
) -> napi_status {
let env = &mut *env;
if env.open_handle_scopes == 0 {
return napi_handle_scope_mismatch;
}
let _scope = &mut *(scope as *mut v8::HandleScope);
// TODO: We are not opening a handle scope, therefore we cannot close it
// TODO: this is also not implemented in napi_open_handle_scope
// let _scope = &mut *(scope as *mut v8::HandleScope);
env.open_handle_scopes -= 1;
napi_ok
}
@ -2381,6 +2383,7 @@ fn napi_open_handle_scope(
) -> napi_status {
let env = &mut *env;
// TODO: this is also not implemented in napi_close_handle_scope
// *result = &mut env.scope() as *mut _ as napi_handle_scope;
env.open_handle_scopes += 1;
napi_ok