Skip to content

Commit 672a03e

Browse files
committed
fix: add .catch() to fire-and-forget setMeta and track error fingerprints
Prevent unhandled promise rejection crash when Redis is down during Pub/Sub error handling. Track error fingerprint so changed error types are re-logged instead of silently suppressed. Add clarifying comments and align test helper with constructor-initialized properties.
1 parent 016ae0a commit 672a03e

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

lib/oauth/pubsub/google.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ class PubSubInstance {
1818
this.stopped = false;
1919
this.recoveryAttempts = 0;
2020
this.lastRecoveryAttempt = 0;
21+
// Initialize to true so the first successful pull clears any stale
22+
// pubSubFlag left in Redis by a previously crashed process
2123
this._hadPubSubFlag = true;
22-
this._loopErrorLogged = false;
24+
this._lastLoopError = null;
2325

2426
this.checkSchemaVersions()
2527
.catch(err => {
@@ -38,21 +40,26 @@ class PubSubInstance {
3840
}
3941
this.run()
4042
.then(() => {
41-
this._loopErrorLogged = false;
43+
this._lastLoopError = null;
4244
this.startLoop();
4345
})
4446
.catch(err => {
45-
if (!this._loopErrorLogged) {
47+
let errKey = [err.message, err.code, err.statusCode].filter(val => val).join('|');
48+
if (this._lastLoopError !== errKey) {
4649
logger.error({ msg: 'Failed to process subscription loop', app: this.app, err });
47-
this._loopErrorLogged = true;
50+
this._lastLoopError = errKey;
4851

4952
this._hadPubSubFlag = true;
50-
oauth2Apps.setMeta(this.app, {
51-
pubSubFlag: {
52-
message: `Failed to process subscription loop`,
53-
description: [err.message, err.reason, err.code].filter(val => val).join('; ')
54-
}
55-
});
53+
oauth2Apps
54+
.setMeta(this.app, {
55+
pubSubFlag: {
56+
message: `Failed to process subscription loop`,
57+
description: [err.message, err.reason, err.code].filter(val => val).join('; ')
58+
}
59+
})
60+
.catch(metaErr => {
61+
logger.error({ msg: 'Failed to update pubSubFlag', app: this.app, err: metaErr });
62+
});
5663
}
5764
setTimeout(() => this.startLoop(), err.retryDelay || 3000);
5865
});

lib/oauth2-apps.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,8 @@ class OAuth2AppsHandler {
851851
};
852852
}
853853

854+
// Note: not atomic (read-modify-write). Concurrent callers use last-write-wins,
855+
// which is acceptable for self-correcting informational metadata.
854856
async setMeta(id, meta) {
855857
let existingMeta;
856858
let existingMetaBuf = await this.redis.hgetBuffer(this.getDataKey(), `${id}:meta`);

test/pubsub-recovery-test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ function createTestInstance(overrides) {
238238
stopped: false,
239239
recoveryAttempts: 0,
240240
lastRecoveryAttempt: 0,
241+
_hadPubSubFlag: true,
242+
_lastLoopError: null,
241243
parent: { getSubscribersKey: () => 'ee:oapp:sub', remove: () => {} },
242244
appData: { id: 'test-app', pubSubSubscription: 'projects/test/subscriptions/test-sub' },
243245
client: {

0 commit comments

Comments
 (0)