-
Notifications
You must be signed in to change notification settings - Fork 30.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #1512 #1563
Fix #1512 #1563
Conversation
Dispatch requests in the implementation of the stream, not in the code creating these requests. The requests might be piled up and invoked internally in the implementation, so it should know better when it is the time to dispatch them. In fact, TLS was doing exactly this thing which led us to... Fix: nodejs#1512
Make sure that no WriteItem's callback will be invoked synchronously. Doing so may lead to the use of uninitialized `req` object, or even worse use-after-free in the caller code. Fix: nodejs#1512
Rebuilt again with this PR, no crashes 👍 |
LGTY anyone? cc @shigeki @bnoordhuis @iojs/collaborators |
@indutny I reviewed the patch and made several tests on Win and Linux. The issue was resolved with this. But I still could not figure out the reason why series of |
Perhaps the roots are in libuv, and the difference in the way it reports error on windows and on unix. /cc @piscisaureus @bnoordhuis @saghul |
@shigeki and thank you for taking a look |
@indutny can you brief me on the core issue? |
Oh right. Basically, |
I suspect that it could be that windows always invokes the syscall from the main thread, while unix does it off-thread when the queue is not empty. But I can't believe that the queue was full all the time. Maybe it is about the way unix reports errors like ECONNRESET? |
@indutny Yeah. Even when Unix does the write on the spot (because the write queue is empty) the error is reported on the next loop iteration: https://github.com/libuv/libuv/blob/v1.x/src/unix/stream.c#L1381 On Windows, however we don't queue the writes, the kernel does. If the call to WSASend fails, however, we return the error immediately. Can you try this (untested) patch? https://gist.github.com/saghul/e05b99b1b16a7c9c15e9 At any rate, I'd say we should handle this in libuv, uv_write should only return the following errors:
|
@saghul unfortunately, it fails to compile:
|
Ah, that patch was done against latest libuv, I'll make one against 1.4.2 for you to test. |
@silverwind here is a patch which should compile with libuv 1.4.2: https://gist.github.com/saghul/52677dba652ab8e78051 |
@saghul thanks, the patch works fine and looks to fix the crashes too. |
@silverwind Fantastic! I'll open an issue over at libuv so we can land this properly. |
I suggest landing this PR anyway. @silverwind, @shigeki LGTY? |
This is a critical bug, so I'm fine with landing a temporary fix for it, to be removed in the next libuv release. Can't really review the code, sorry. |
This is not a temporary fix. The error wasn't properly handed anyway, it is the bad libuv behavior the exposed the problem, but it was here for a long time anyway. |
@indutny The fix looks kinda obvious, but a LGTM from @piscisaureus would be nice. |
@piscisaureus and this one? ^ |
@indutny it's the same fix. We changed the internal structure to not use an anonymous union. |
@saghul I meant reviewing this PR... |
@piscisaureus the commit log is borked, I'll rewrite it. Thanks for the review! |
@saghul Thanks for the patch. I really understand this behavior on Windows. It is very helpful for me. @indutny I have one more question. Do sync/async checks need per diff --git a/src/tls_wrap.cc b/src/tls_wrap.cc
index c774a84..1bd1f05 100644
--- a/src/tls_wrap.cc
+++ b/src/tls_wrap.cc
@@ -117,6 +117,23 @@ bool TLSWrap::InvokeQueued(int status) {
}
+void TLSWrap::CheckInvokeQueued::CheckCb(uv_check_t* check) {
+ CheckInvokeQueued* c = reinterpret_cast<CheckInvokeQueued*>(check->data);
+ c->w_->InvokeQueued(c->status_);
+ uv_close(reinterpret_cast<uv_handle_t*>(check), nullptr);
+ delete c;
+}
+
+
+int TLSWrap::InvokeQueuedAsync(int status) {
+ CheckInvokeQueued* check = new CheckInvokeQueued(this, status);
+ uv_check_init(env()->event_loop(), &check->check_);
+ check->check_.data = check;
+ int err = uv_check_start(&check->check_, CheckInvokeQueued::CheckCb);
+ return err;
+}
+
+
void TLSWrap::NewSessionDoneCb() {
Cycle();
}
@@ -311,7 +328,7 @@ void TLSWrap::EncOut() {
// Ignore errors, this should be already handled in js
if (err) {
write_req->Dispose();
- InvokeQueued(err);
+ InvokeQueuedAsync(err);
} else {
NODE_COUNT_NET_BYTES_SENT(write_size_);
}
diff --git a/src/tls_wrap.h b/src/tls_wrap.h
index 9f09535..de7140f 100644
--- a/src/tls_wrap.h
+++ b/src/tls_wrap.h
@@ -62,6 +62,22 @@ class TLSWrap : public crypto::SSLWrap<TLSWrap>,
// Maximum number of buffers passed to uv_write()
static const int kSimultaneousBufferCount = 10;
+ class CheckInvokeQueued {
+ public:
+ CheckInvokeQueued(TLSWrap* w, int status) : w_(w), status_(status) {
+ }
+
+ ~CheckInvokeQueued() {
+ w_ = nullptr;
+ }
+
+ static void CheckCb(uv_check_t* check);
+
+ TLSWrap* w_;
+ int status_;
+ uv_check_t check_;
+ };
+
// Write callback queue's item
class WriteItem {
public:
@@ -88,6 +104,7 @@ class TLSWrap : public crypto::SSLWrap<TLSWrap>,
void ClearOut();
void MakePending();
bool InvokeQueued(int status);
+ int InvokeQueuedAsync(int status);
inline void Cycle() {
// Prevent recursion I've just borrowed your code. If it is right, I think it can reduce the costs to check that if every |
I might be completely off here... but is this really needed if libuv does The Right Thing (TM)? If uv_write only fails with the errors I mentioned here: #1563 (comment) then, from iojs' prespective, those are programming errors and you could abort(). In all other cases the callback would be called in the future, as expected. |
@saghul Yes, I agree it. After fixing libuv, only ENOMEM, EBADF or ENOBUFS returns from |
Hm... if you insist on it.... Let's land the first commit then. |
Any objections? |
No, objection. LGTM. |
Dispatch requests in the implementation of the stream, not in the code creating these requests. The requests might be piled up and invoked internally in the implementation, so it should know better when it is the time to dispatch them. In fact, TLS was doing exactly this thing which led us to... Fix: #1512 PR-URL: #1563 Reviewed-By: Shigeki Ohtsu <ohtsu@iij.ad.jp>
Landed in 30b7349, thank you! |
PR-URL: #1532 Notable Changes: * crypto: significantly reduced memory usage for TLS (Fedor Indutny & Сковорода Никита Андреевич) #1529 * net: socket.connect() now accepts a 'lookup' option for a custom DNS resolution mechanism, defaults to dns.lookup() (Evan Lucas) #1505 * npm: Upgrade npm to 2.9.0. See the v2.8.4 and v2.9.0 release notes for details. Notable items: - Add support for default author field to make npm init -y work without user-input (@othiym23) npm/npm/d8eee6cf9d - Include local modules in npm outdated and npm update (@ArnaudRinquin) npm/npm#7426 - The prefix used before the version number on npm version is now configurable via tag-version-prefix (@kkragenbrink) npm/npm#8014 * os: os.tmpdir() is now cross-platform consistent and will no longer returns a path with a trailling slash on any platform (Christian Tellnes) #747 * process: - process.nextTick() performance has been improved by between 2-42% across the benchmark suite, notable because this is heavily used across core (Brian White) #1548 - New process.geteuid(), process.seteuid(id), process.getegid() and process.setegid(id) methods allow you to get and set effective UID and GID of the process (Evan Lucas) #1536 * repl: - REPL history can be persisted across sessions if the NODE_REPL_HISTORY_FILE environment variable is set to a user accessible file, NODE_REPL_HISTORY_SIZE can set the maximum history size and defaults to 1000 (Chris Dickinson) #1513 - The REPL can be placed in to one of three modes using the NODE_REPL_MODE environment variable: sloppy, strict or magic (default); the new magic mode will automatically run "strict mode only" statements in strict mode (Chris Dickinson) #1513 * smalloc: the 'smalloc' module has been deprecated due to changes coming in V8 4.4 that will render it unusable * util: add Promise, Map and Set inspection support (Christopher Monsanto) #1471 * V8: upgrade to 4.2.77.18, see the ChangeLog for full details. Notable items: - Classes have moved out of staging; the class keyword is now usable in strict mode without flags - Object literal enhancements have moved out of staging; shorthand method and property syntax is now usable ({ method() { }, property }) - Rest parameters (function(...args) {}) are implemented in staging behind the --harmony-rest-parameters flag - Computed property names ({['foo'+'bar']:'bam'}) are implemented in staging behind the --harmony-computed-property-names flag - Unicode escapes ('\u{xxxx}') are implemented in staging behind the --harmony_unicode flag and the --harmony_unicode_regexps flag for use in regular expressions * Windows: - Random process termination on Windows fixed (Fedor Indutny) #1512 / #1563 - The delay-load hook introduced to fix issues with process naming (iojs.exe / node.exe) has been made opt-out for native add-ons. Native add-ons should include 'win_delay_load_hook': 'false' in their binding.gyp to disable this feature if they experience problems . (Bert Belder) #1433 * Governance: - Rod Vagg (@rvagg) was added to the Technical Committee (TC) - Jeremiah Senkpiel (@Fishrock123) was added to the Technical Committee (TC)
I suggest landing the second part of this changeset too, at least until it'll be fixed in libuv. |
PTAL |
The patch will land as is plus a test on libuv, so I guess you could float
|
@silverwind nope, it is good to have anyway. |
Alright! |
Dispatch requests in the implementation of the stream, not in the code creating these requests. The requests might be piled up and invoked internally in the implementation, so it should know better when it is the time to dispatch them. In fact, TLS was doing exactly this thing which led us to... Fix: nodejs#1512 PR-URL: nodejs#1563 Reviewed-By: Shigeki Ohtsu <ohtsu@iij.ad.jp>
n/t
See #1558