From 19b4c27ebf6d0b1aa2ded64d57fa44ec70e8756e Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Wed, 16 Feb 2011 21:09:43 -0500 Subject: [PATCH] TLS: Make _cycle reentrant. --- lib/tls.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/tls.js b/lib/tls.js index cbe706b8af9..9760490b614 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(); } };