Skip to content
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

How to exit after calling the request? #711

Closed
luchenqun opened this issue Jul 4, 2023 · 4 comments · Fixed by #716
Closed

How to exit after calling the request? #711

luchenqun opened this issue Jul 4, 2023 · 4 comments · Fixed by #716
Labels
bug Something isn't working

Comments

@luchenqun
Copy link

luchenqun commented Jul 4, 2023

Describe the bug
I used Node.js to call a gRPC service with an official example. However, after executing the script, Node.js did not exit and I had to manually call process.exit(0) to exit. Below is the relevant JavaScript code I wrote:

import { createPromiseClient } from "@bufbuild/connect";
import { ElizaService } from "@buf/bufbuild_eliza.bufbuild_connect-es/buf/connect/demo/eliza/v1/eliza_connect.js";
import { createGrpcTransport } from "@bufbuild/connect-node";
const transport = createGrpcTransport({
  baseUrl: "https://demo.connect.build",
  httpVersion: "2",
});

async function main() {
  const client = createPromiseClient(ElizaService, transport);
  const res = await client.say({ sentence: "I feel happy." });
  console.log(res);
  // process.exit(0);
}
main();

Environment (please complete the following information):

  • @bufbuild/connect-node version: 0.11.0
  • Node.js version: 16.16.0

The program will automatically exit in about ten minutes.exited with code=0 in 611.145 seconds

@luchenqun luchenqun added the bug Something isn't working label Jul 4, 2023
@emcfarlane
Copy link

emcfarlane commented Jul 4, 2023

Extending the script with:

await main();

const activeHandles = process._getActiveHandles();
const activeRequests = process._getActiveRequests();
console.log("activeHandles", activeHandles.length, activeHandles);
console.log("activeRequest", activeRequests.length, activeRequests);

shows 3 activeHandles, 2 WriteStream and 1 TLSSocket.

Exits after 10m, I think its a connection pool that is keeping the script from closing.

@luchenqun
Copy link
Author

Extending the script with:

await main();

const activeHandles = process._getActiveHandles();
const activeRequests = process._getActiveRequests();
console.log("activeHandles", activeHandles.length, activeHandles);
console.log("activeRequest", activeRequests.length, activeRequests);

shows 3 activeHandles, 2 WriteStream and 1 TLSSocket.

Exits after 10m, I think its a connect pool that is keeping the script from closing.

Thank you for your answer. If it's a connection pool, can we safely exit the program by closing the connection pool?

@timostamm
Copy link
Member

Hey @luchenqun, with v0.10.0, clients keep the HTTP/2 connection alive. Node.js respects the active socket and does not exit on it's own.

The default timeout for idle connections is 15 minutes. You can provide a smaller value to let your script exit earlier, for example 5 seconds:

const transport = createGrpcTransport({
  baseUrl: "https://demo.connect.build",
  httpVersion: "2",
+ idleConnectionTimeoutMs: 5000,
});

If waiting for the idle timeout is not helpful in your use case, you can close the connection explicitly. You can find an example at the bottom of this PR description: #673

While this works as intended, it might be surprising without the context. I think we might want to unref the connection so that the code in your example would exit right away.

@timostamm
Copy link
Member

The enhancement to this behavior was released in v0.12.0. The example in the description should exit on its own now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants