Skip to content

Commit 089438b

Browse files
committed
Do not try to send GOODBYE message when connection is broken
To avoid unnecessary "write after end" errors when connection has been closed because of an error.
1 parent 0e25da5 commit 089438b

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/v1/internal/connection.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,9 @@ export default class Connection {
387387
this._log.debug(`${this} closing`);
388388
}
389389

390-
if (this._protocol) {
391-
// protocol has been initialized
392-
// use it to notify the database about the upcoming close of the connection
390+
if (this._protocol && this.isOpen()) {
391+
// protocol has been initialized and this connection is healthy
392+
// notify the database about the upcoming close of the connection
393393
this._protocol.prepareToClose(NO_OP_OBSERVER);
394394
}
395395

test/internal/connection.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,27 @@ describe('Connection', () => {
367367
}).catch(done.fail);
368368
});
369369

370+
it('should not prepare broken connection to close', done => {
371+
connection = createConnection('bolt://localhost');
372+
373+
connection.connect('my-connection/9.9.9', basicAuthToken())
374+
.then(() => {
375+
expect(connection._protocol).toBeDefined();
376+
expect(connection._protocol).not.toBeNull();
377+
378+
// make connection seem broken
379+
connection._isBroken = true;
380+
expect(connection.isOpen()).toBeFalsy();
381+
382+
connection._protocol.prepareToClose = () => {
383+
throw new Error('Not supposed to be called');
384+
};
385+
386+
connection.close(() => done());
387+
})
388+
.catch(error => done.fail(error));
389+
});
390+
370391
function packedHandshakeMessage() {
371392
const result = alloc(4);
372393
result.putInt32(0, 1);

0 commit comments

Comments
 (0)