Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit 14a8fb8

Browse files
committed
tls: write pending data of opposite side
Fix stucked CryptoStream behaviour, happening when one of the sides locks-up in queued state. fix #5023
1 parent 94284e7 commit 14a8fb8

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

lib/tls.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ CryptoStream.prototype._read = function read(size) {
435435

436436
// Try writing pending data
437437
if (this._pending !== null) this._writePending();
438+
if (this._opposite._pending !== null) this._opposite._writePending();
438439

439440
if (bytesRead === 0) {
440441
// EOF when cleartext has finished and we have nothing to read
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var tls = require('tls');
25+
var fs = require('fs');
26+
var stream = require('stream');
27+
var util = require('util');
28+
29+
var clientConnected = 0;
30+
var serverConnected = 0;
31+
var request = new Buffer(new Array(1024 * 256).join('ABCD')); // 1mb
32+
33+
var options = {
34+
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),
35+
cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem')
36+
};
37+
38+
function Mediator() {
39+
stream.Writable.call(this);
40+
this.buf = '';
41+
};
42+
util.inherits(Mediator, stream.Writable);
43+
44+
Mediator.prototype._write = function write(data, enc, cb) {
45+
this.buf += data;
46+
setTimeout(cb, 0);
47+
48+
if (this.buf.length >= request.length) {
49+
assert.equal(this.buf, request.toString());
50+
server.close();
51+
}
52+
};
53+
54+
var mediator = new Mediator();
55+
56+
var server = tls.Server(options, function(socket) {
57+
socket.pipe(mediator);
58+
serverConnected++;
59+
});
60+
61+
server.listen(common.PORT, function() {
62+
var client1 = tls.connect({
63+
port: common.PORT,
64+
rejectUnauthorized: false
65+
}, function() {
66+
++clientConnected;
67+
client1.end(request);
68+
});
69+
});
70+
71+
process.on('exit', function() {
72+
assert.equal(clientConnected, 1);
73+
assert.equal(serverConnected, 1);
74+
});

0 commit comments

Comments
 (0)