Skip to content

Commit 04c8d25

Browse files
committed
Reject currentRequest onClose to handle ECONNRESET
1 parent c1847f0 commit 04c8d25

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/client-request-handler.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ export default abstract class MBClientRequestHandler<S extends Stream.Duplex, Re
186186

187187
protected _onClose () {
188188
this._state = 'offline'
189+
this._currentRequest && this._currentRequest.reject(new UserRequestError({
190+
err: OFFLINE,
191+
message: 'connection to modbus server closed'
192+
}))
189193
this._clearAllRequests()
190194
}
191195

test/tcp-client.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,22 @@ describe('TCP Client Tests.', function () {
122122
done()
123123
})
124124
})
125+
it('should handle a valid request with read ECONNRESET', function (done) {
126+
socket.write = function() {
127+
// Socket receives a read ECONNRESET, the socket is closed and no further data is received
128+
socket.emit('close');
129+
}
130+
const client = new Modbus.client.TCP(socket, 2, 100) // unit id = 2, timeout = 100ms
131+
socket.emit('connect')
132+
133+
client.readCoils(10, 11)
134+
.then(function (resp) {
135+
assert.ok(false)
136+
}).catch(function (e) {
137+
assert.equal('Offline', e.err)
138+
done()
139+
})
140+
})
125141
it('should handle a valid request while offline', function (done) {
126142
const client = new Modbus.client.TCP(socket)
127143

0 commit comments

Comments
 (0)