Skip to content

Commit

Permalink
Fix socket tests with connection errors (#2403)
Browse files Browse the repository at this point in the history
* release pending activity with connection error handler

* unref poll_ref
  • Loading branch information
dylan-conway authored Mar 16, 2023
1 parent b66df90 commit d5ef247
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/bun.js/api/bun/socket.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,8 @@ fn NewSocket(comptime ssl: bool) type {
var promise = val.asPromise().?;
const err_ = err.toErrorInstance(globalObject);
promise.rejectOnNextTickAsHandled(globalObject, err_);
this.has_pending_activity.store(false, .Release);
this.poll_ref.unref(handlers.vm);
}
}

Expand Down
10 changes: 9 additions & 1 deletion test/js/bun/net/socket.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, it } from "bun:test";
import { bunExe } from "harness";
import { bunExe, expectObjectTypeCount } from "harness";
import { connect, spawn } from "bun";

it("should keep process alive only when active", async () => {
Expand Down Expand Up @@ -91,6 +91,10 @@ it("should reject on connection error, calling both connectError() and rejecting
);
});

it("should not leak memory when connect() fails", async () => {
await expectObjectTypeCount("TCPSocket", 1, 100);
});

// this also tests we mark the promise as handled if connectError() is called
it("should handle connection error", done => {
var data = {};
Expand Down Expand Up @@ -128,3 +132,7 @@ it("should handle connection error", done => {
},
});
});

it("should not leak memory when connect() fails again", async () => {
await expectObjectTypeCount("TCPSocket", 1, 100);
});

0 comments on commit d5ef247

Please sign in to comment.