ping() never returns if database is down
Whenever mongo database is down ping() method never returns. This prevents implementing health checks on APIs with mongo datasource. There was a bug reported for this issue back in 2014 and was marked as fixed. However I'm seeing that this issue is still there.
Steps to reproduce
- Create a Loopback 4 application with mongodb as datasource
- Implement live/ready healthcheck endpoint as described here which calls ds.ping()
- Take mongo database down and hit /live endpoint. The request never completes
Related Issues
https://github.com/strongloop/loopback-connector-mongodb/issues/47
@emonddr, could you please take a look? Thanks.
I just tried the steps above.
When the mongo db is down:
- the very 1st request : GET /live takes 2 minutes but does complete with an empty result
- the 2nd request completes immediately
$ curl -X GET "http://[::1]:3000/live" -H "accept: application/json" | jq
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- 0:02:00 --:--:-- 0
curl: (52) Empty reply from server
$ curl -X GET "http://[::1]:3000/live" -H "accept: application/json" | jq
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 199 100 199 0 0 24875 0 --:--:-- --:--:-- --:--:-- 24875
{
"status": "DOWN",
"checks": [
{
"name": "health.MyLiveCheck",
"state": "UP",
"data": {
"reason": ""
}
},
{
"name": "health.MyDBCheck",
"state": "DOWN",
"data": {
"reason": "pool is draining, new operations prohibited"
}
}
]
}
Even if I restart the docker container which has the mongo db,
any new requests still show the DB as down
$ curl -X GET "http://[::1]:8050/live" -H "accept: application/json" | jq
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 170 100 170 0 0 21250 0 --:--:-- --:--:-- --:--:-- 21250
{
"status": "DOWN",
"checks": [
{
"name": "health.MyLiveCheck",
"state": "UP",
"data": {
"reason": ""
}
},
{
"name": "health.MyDBCheck",
"state": "DOWN",
"data": {
"reason": "pool destroyed"
}
}
]
}
I have to stop the LB4 app, and restart it.
Then I perform another test, and it finally shows the DB is up
$ curl -X GET "http://[::1]:8050/live" -H "accept: application/json" | jq
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 152 100 152 0 0 2576 0 --:--:-- --:--:-- --:--:-- 2576
{
"status": "UP",
"checks": [
{
"name": "health.MyLiveCheck",
"state": "UP",
"data": {
"reason": ""
}
},
{
"name": "health.MyDBCheck",
"state": "UP",
"data": {
"reason": ""
}
}
]
}
I actually had to define my provider like this:
// Define a provider to check the liveness of a datasource
class DBLiveCheckProvider implements Provider<LiveCheck> {
constructor(@inject('datasources.db') private ds: DataSource) {}
value() {
return async () => {
await this.ds.ping();
}
}
}
instead of
// Define a provider to check the liveness of a datasource
class DBLiveCheckProvider implements Provider<LiveCheck> {
constructor(@inject('datasources.db') private ds: DataSource) {}
value() {
return () => this.ds.ping();
}
}
Otherwise my VSCode editor would complain about this value() method:

@dkrantsberg, did the comments from @emonddr resolve your issue? If yes, please close this issue. Thanks.
@dhmlau No, wrapping the call to the ping() function in an async function doesn't make any difference. The behavior is still the same:
- After mongo container stopped the first call to db.ping() never returns (neither resolves nor rejects).
- After some however (maybe ~30 seconds) subsequent calls to db.ping() get rejected fast with 'pool destroyed' error
- Even after mongo is brought back up db.ping() rejects with the same 'pool destroyed' error
- Only restarting the LB4 app makes db.ping() succeed again
@dkrantsberg Have you tried to tune the datasource setting for reconnect per https://mongodb.github.io/node-mongodb-native/3.5/reference/connecting/connection-settings/?
Closing due to inactivity. Please create a new issue if you are still running into problems.