loopbackio / loopbackio/loopback-next

Database connect error during app boot should not terminate node process

Open
#8,639 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Repository
Dominant language
TypeScript
Stars
5.1k
Forks
1.1k
Avg merge
2d 21h
Merged PRs (30d)
27

Description

Describe the bug
Steps to repro:
  1. Scaffold new app (lb4 app)
  2. Scaffold new mysql data source (lb4 datasource). Enter invalid connection info, so that the connection fails during boot.
  3. Build and run the app
Expected:

Server starts successfully, and /ping endpoint may be ran.

Actual:

Server fails to boot and the process terminates

Alternative with lazy connect:

If "lazy connect" is enabled on the data source config, and a manual datasource.connect() is made later on, the error will cause an unhandled exception to crash the node server. Be aware that trying to run datasource.execute() directly without a connected data source will return a promise that will never resolve until a connection is made.

Note that this occurs even if the call is wrapped in a try/catch.
Further, normal crud repository fetchers, which internally queue the connect, are unaffected.

example:

{
   ...other datasource props,
   lazyConnect: true
}
@get('/test')
async test() {
  try {
    //execute will only queue, if not connected, causing an infinite wait
    if (!this.dataSource.connected)
      await this.dataSource.connect();

    //execute custom sql
    await this.dataSource.execute("select 1");
    return true;
  } catch(ex) {
    return false;
  }
}
Use Case:

Health check endpoints that check if parts of the application are online, including the database.

It is vital that the api continue to run, even if a dependency is unavailable. If a datasource.execute() or datasource.connect() fails inside a controller method, then it should not tear down the server with it!

Cli Version: 3.2.0

Logs

No response

Additional information

No response

Reproduction

repro provided above

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the datasource.connect() and datasource.execute() paths described in the reproduction, including their use during application boot and from the controller example. Reproduce the invalid MySQL connection with lazyConnect enabled, then verify that boot and controller-level failures are handled without terminating the Node process or leaving the request unresolved.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, nodejs, typescript
Domain
api, backend, database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.