diff options
-rw-r--r-- | lib/tls.js | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/lib/tls.js b/lib/tls.js index cbe706b8a..9760490b6 100644 --- a/lib/tls.js +++ b/lib/tls.js @@ -69,7 +69,9 @@ CryptoStream.prototype.write = function(data /* , encoding, cb */) { this._pending.push(data); this._pendingCallbacks.push(cb); + this.pair._writeCalled = true; this.pair._cycle(); + return this._writeState; }; @@ -507,6 +509,11 @@ SecurePair.prototype._cycle = function() { return; } + // Make this function reentrant. + if (this._cycleLock) return; + this._cycleLock = true; + this._writeCalled = false; + var established = this._secureEstablished; this.encrypted._pull(); @@ -514,8 +521,15 @@ SecurePair.prototype._cycle = function() { this.cleartext._push(); this.encrypted._push(); - if (!established && this._secureEstablished) { + this._cycleLock = false; + + if (this._done) { + return; + } + + if ((!established && this._secureEstablished) || this._writeCalled) { // If we were not established but now we are, let's cycle again. + // Or if there is some data to write... this._cycle(); } }; |