Skip to content

Process hangs when running connection.close() using Bun #1681

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

Open
Publicker opened this issue Mar 17, 2025 · 0 comments
Open

Process hangs when running connection.close() using Bun #1681

Publicker opened this issue Mar 17, 2025 · 0 comments

Comments

@Publicker
Copy link

Software versions

  • Tedious: 18.6.1
  • SQL Server: Microsoft Azure SQL Edge Developer (RTM) - 15.0.2000.1574 (ARM64)
  • Node.js: 22.12.0
  • Bun: 1.2.5

Additional Libraries Used and Versions

  • Bun - used as the package manager and runtime.

Table schema
N/A

Connection configuration

{
  server: "localhost",
  authentication: {
    type: "default",
    options: { userName: "sa", password: "Password123" },
  },
  options: {
    database: "MyMssqlDb",
    trustServerCertificate: true,
  },
}

Problem description
When running a simple connection and disconnection using Bun, the process remains active indefinitely. I waited for more than 5 minutes, but it did not terminate.

This behavior suggests that some resources might not be properly disposed of in Bun, whereas Node handles it differently (perhaps more effectively).

Expected behavior
After calling connection.close(), the process should terminate.

Actual behavior
Even after closing the connection, the process continues to hang indefinitely.

Error message/stack trace
N/A

Any other details that can be helpful
After investigating the issue, I suspect that the securePair sockets (securePair.encrypted and securePair.cleartext) are not being properly destroyed when the socket closes.

Interestingly, this issue does not occur when using Node.js, which may indicate an underlying problem with lingering open sockets that is not as noticeable in Node.

I was able to resolve the issue by manually destroying the sockets when the end event is emitted on the connection:

connection.on("end", function (err) {
  if (err) {
    console.log("end error: ", err);
    return;
  }

  // 👇 This fixes the issue
  connection.messageIo.securePair.encrypted.destroy();
  connection.messageIo.securePair.cleartext.destroy();

  console.log("end OK");
});

Proposed Fix
If you believe this fix is appropriate, I can submit a PR. The proposed change involves explicitly destroying the secure pair sockets when the main socket emits end, close, or error.

Suggested code modification in tedious/src/message-io.ts:

Define a cleanup method:

private cleanupSecurePair() {
  // End/destroy secure pair sockets
  this.securePair?.encrypted.destroy();
  this.securePair?.cleartext.destroy();
}

Attach it to the socket lifecycle events:

// Handle main socket closure
this.socket.on('end', this.cleanupSecurePair);
this.socket.on('error', this.cleanupSecurePair);
this.socket.on('close', this.cleanupSecurePair);

Would this approach be acceptable? If so, I’d be happy to submit a PR.

Comparing.connection.with.Node.and.Bun.mp4
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

No branches or pull requests

1 participant