Skip to content

Commit cc51dff

Browse files
authored
Bugfix: timeout error crashing explorer (#253)
Signed-off-by: Thomas <[email protected]>
1 parent 4a931ca commit cc51dff

File tree

4 files changed

+50
-18
lines changed

4 files changed

+50
-18
lines changed

app/platform/fabric/gateway/FabricGateway.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -434,13 +434,26 @@ export class FabricGateway {
434434
if (!this.waitingResp) {
435435
this.waitingResp = true;
436436
logger.info('Sending discovery request...');
437-
await this.ds.send({
438-
asLocalhost: this.asLocalhost,
439-
requestTimeout: 5000,
440-
refreshAge: 15000,
441-
targets: this.dsTargets
442-
});
443-
logger.info('Succeeded to send discovery request');
437+
await this.ds
438+
.send({
439+
asLocalhost: this.asLocalhost,
440+
requestTimeout: 5000,
441+
refreshAge: 15000,
442+
targets: this.dsTargets,
443+
})
444+
.then(() => {
445+
logger.info('Succeeded to send discovery request');
446+
})
447+
.catch(error => {
448+
if (error) {
449+
logger.warn(
450+
'Failed to send discovery request for channel',
451+
error,
452+
);
453+
this.waitingResp = false;
454+
this.ds.close();
455+
}
456+
});
444457
} else {
445458
logger.info('Have already been sending a request');
446459
return null;

app/platform/fabric/sync/SyncService.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,15 @@ export class SyncServices {
360360
async syncBlocks(client, channel_name) {
361361
const network_id = client.getNetworkId();
362362

363+
// Get channel information from ledger
364+
const channelInfo = await client.fabricGateway.queryChainInfo(channel_name);
365+
366+
if (!channelInfo) {
367+
logger.info(
368+
`syncBlocks: Failed to retrieve channelInfo >> ${channel_name}`,
369+
);
370+
return;
371+
}
363372
const synch_key = `${network_id}_${channel_name}`;
364373
logger.info(`syncBlocks: Start >> ${synch_key}`);
365374
if (this.synchInProcess.includes(synch_key)) {
@@ -368,8 +377,6 @@ export class SyncServices {
368377
}
369378
this.synchInProcess.push(synch_key);
370379

371-
// Get channel information from ledger
372-
const channelInfo = await client.fabricGateway.queryChainInfo(channel_name);
373380
const channel_genesis_hash = client.getChannelGenHash(channel_name);
374381
const blockHeight = parseInt(channelInfo.height.low) - 1;
375382
// Query missing blocks from DB

app/sync.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ process.on('unhandledRejection', (up : {message : string}) => {
5858
} else {
5959
logger.error(up);
6060
}
61-
shutDown();
61+
// prevent timeout error from calling shutdown
62+
if (!up.message.includes('REQUEST TIMEOUT')) {
63+
shutDown();
64+
}
6265
});
6366
process.on('uncaughtException', up => {
6467
logger.error(

app/test/FabricGateway.test.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import sinon from 'sinon';
1010
import { FabricGateway } from '../platform/fabric/gateway/FabricGateway';
1111

1212
// DiscoveryService (this.ds)
13+
const stubSend = sinon.stub();
1314
const stubSign = sinon.stub();
1415
const stubGetDiscoveryResults = sinon.stub();
1516
const stubClose = sinon.stub();
@@ -35,7 +36,7 @@ function getFabricGatewayInstance() {
3536
return {
3637
build: sinon.stub(),
3738
sign: stubSign,
38-
send: sinon.stub(),
39+
send: stubSend,
3940
getDiscoveryResults: stubGetDiscoveryResults,
4041
close: stubClose
4142
};
@@ -91,6 +92,7 @@ function getFabricGatewayInstance() {
9192

9293
function resetAllStubs() {
9394
// DiscoveryService (this.ds)
95+
stubSend.reset();
9496
stubSign.reset();
9597
stubGetDiscoveryResults.reset();
9698
stubClose.reset();
@@ -173,18 +175,25 @@ describe('sendDiscoveryRequest', () => {
173175
resetAllStubs();
174176
});
175177

178+
it('should throw error when discoveryService.sends() throw error', async () => {
179+
stubSend.rejects(Promise.reject(new Error('REQUEST TIMEOUT')));
180+
await gw.sendDiscoveryRequest();
181+
expect(stubWarn.called).be.equal(true);
182+
expect(stubClose.calledOnce).be.equal(true);
183+
});
184+
185+
it('should throw error when failed to call getDiscoveryResults()', async () => {
186+
stubGetDiscoveryResults.throws();
187+
await gw.sendDiscoveryRequest();
188+
expect(stubWarn.called).be.equal(true);
189+
expect(stubClose.calledOnce).be.equal(true);
190+
});
191+
176192
it('should return without error', async () => {
177193
stubGetDiscoveryResults.returns(Promise.resolve());
178194
await gw.sendDiscoveryRequest();
179195
expect(stubError.called).be.equal(false);
180196
});
181-
182-
it('should throw error when failed to call getDiscoveryResults()', async () => {
183-
stubGetDiscoveryResults.throws();
184-
await gw.sendDiscoveryRequest();
185-
expect(stubWarn.called).be.equal(true);
186-
expect(stubClose.calledOnce).be.equal(true);
187-
});
188197
});
189198

190199
describe('getDiscoveryResult', () => {

0 commit comments

Comments
 (0)