question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Graceful shutdown

See original GitHub issue

Problem

I’d like to shutdown my application gracefully and do some database work before I exit the process myself.

However, Prisma exits the process on a SIGINT event on its own. Even though I got a work-around by monkey-patching “process.exit”, the Prisma query engine still exits with the SIGINT event and I’m unable to execute queries therefore.

The Node.js process already received a SIGINT signal, therefore the Prisma query engine exited and your request can't be processed.

Suggested solution

  1. Make prisma stop listening to process events by setting an option
  2. Stop the engines manually by executing a stop method (?) just after the application is ready to exit

Alternatives

here’s the very hacky work-around that I use (though I’d appreciate something else)

const realProcessExit = process.exit;
process.exit = () => {
  // Monkey-patch process.exit because Prisma wants to exit before
  // we can shut down gracefully
};

import { PrismaClient } from "@prisma/client";

["EXIT", "SIGINT", "SIGUSR1", "SIGUSR2", "uncaughtException", "SIGTERM"].forEach((eventType) => {
  const previousListeners = process.listeners(eventType);
  process.removeAllListeners(eventType);

  process.once(eventType, async (param) => {
    await shutdown();

    // Make prisma stop the engines after the application is ready to exit
    previousListeners.forEach((l) => l(param));

    if (eventType !== "EXIT") {
      realProcessExit();
    }
  });
});

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:10
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

18reactions
microwavekonijncommented, Aug 17, 2021

All I want is to call $disconnect myself even in the case of a SIGINT, that way I have completely predictable behavior for my app(so the app will react to the SIGINT/process closing).

I looked at the code and found that I can just returned a never resolving promise in the callback for beforeExit, blocking the SIGINT behavior.

9reactions
dfeecommented, Oct 12, 2020

The opt-out and callback on exit seems to make sense though. I just discovered that the reason my NestJs app isn’t performing it’s onApplicationShutdown cleanups are that Prisma is killing the process before it gets there.

I know this is slatted for current development, so wanted to get my thoughts in. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is graceful shutdown and hard shutdown? - TechTarget
Graceful shutdown and hard shutdown are two opposing methods of turning off a computer. A graceful shutdown is when a computer is turned...
Read more >
Graceful Shutdown - RisingStack Engineering
Graceful shutdown means that the OS (operating system) can safely shut down its processes and close all connections, however long that takes.
Read more >
Health Checks and Graceful Shutdown - Express.js
Terminus is an open-source project that adds health checks and graceful shutdown to your application to eliminate the need to write boilerplate code....
Read more >
Implementing Graceful Shutdown in Go | RudderStack Blog
Shutdown gracefully shuts down the server without interrupting any active connections. Nice, but how? Shutdown works by first closing all open ...
Read more >
Explain Graceful shutdown in Express.js - GeeksforGeeks
Procedure of Graceful Shutdown: For this purpose, a SIGTERM (the program manager sends it ) signal is sent to the application that tells...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found