fix(postgres) memory fix when connection fails sync (#21616)

### What does this PR do?
We should not call .deinit() after .toJS otherwise hasPendingActivity
will access invalid memory

### How did you verify your code works?
Test run it with debug build on macos or asan on and will catch it

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Ciro Spaciari
2025-08-04 19:41:20 -07:00
committed by GitHub
parent 4568258960
commit 258a2a2e3a
3 changed files with 43 additions and 12 deletions

View File

@@ -717,16 +717,6 @@ pub fn call(globalObject: *jsc.JSGlobalObject, callframe: *jsc.CallFrame) bun.JS
},
};
ptr.updateHasPendingActivity();
ptr.poll_ref.ref(vm);
const js_value = ptr.toJS(globalObject);
js_value.ensureStillAlive();
ptr.js_value = js_value;
js.onconnectSetCached(js_value, globalObject, on_connect);
js.oncloseSetCached(js_value, globalObject, on_close);
bun.analytics.Features.postgres_connections += 1;
{
const hostname = hostname_str.toUTF8(bun.default_allocator);
defer hostname.deinit();
@@ -761,9 +751,18 @@ pub fn call(globalObject: *jsc.JSGlobalObject, callframe: *jsc.CallFrame) bun.JS
},
};
}
ptr.resetConnectionTimeout();
}
// only call toJS if connectUnixAnon does not fail immediately
ptr.updateHasPendingActivity();
ptr.resetConnectionTimeout();
ptr.poll_ref.ref(vm);
const js_value = ptr.toJS(globalObject);
js_value.ensureStillAlive();
ptr.js_value = js_value;
js.onconnectSetCached(js_value, globalObject, on_connect);
js.oncloseSetCached(js_value, globalObject, on_close);
bun.analytics.Features.postgres_connections += 1;
return js_value;
}