| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 940a297 commit 4222f24
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -627,8 +627,6 @@ class QueryWrap : public AsyncWrap { | |||
| 627 | 627 | } else { | |
| 628 | 628 | Parse(response_data_->host.get()); | |
| 629 | 629 | } | |
| 630 | - | ||
| 631 | - delete this; | ||
| 632 | 630 | } | |
| 633 | 631 | ||
| 634 | 632 | void* MakeCallbackPointer() { | |
@@ -686,9 +684,13 @@ class QueryWrap : public AsyncWrap { | |||
| 686 | 684 | } | |
| 687 | 685 | ||
| 688 | 686 | void QueueResponseCallback(int status) { | |
| 689 | - env()->SetImmediate([this](Environment*) { | ||
| 687 | + BaseObjectPtr<QueryWrap> strong_ref{this}; | ||
| 688 | + env()->SetImmediate([this, strong_ref](Environment*) { | ||
| 690 | 689 | AfterResponse(); | |
| 691 | - }, object()); | ||
| 690 | + | ||
| 691 | + // Delete once strong_ref goes out of scope. | ||
| 692 | + Detach(); | ||
| 693 | + }); | ||
| 692 | 694 | ||
| 693 | 695 | channel_->set_query_last_ok(status != ARES_ECONNREFUSED); | |
| 694 | 696 | channel_->ModifyActivityQueryCount(-1); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -746,13 +746,9 @@ inline void IsolateData::set_options( | |||
| 746 | 746 | } | |
| 747 | 747 | ||
| 748 | 748 | template <typename Fn> | |
| 749 | - void Environment::CreateImmediate(Fn&& cb, | ||
| 750 | - v8::Local<v8::Object> keep_alive, | ||
| 751 | - bool ref) { | ||
| 749 | + void Environment::CreateImmediate(Fn&& cb, bool ref) { | ||
| 752 | 750 | auto callback = std::make_unique<NativeImmediateCallbackImpl<Fn>>( | |
| 753 | - std::move(cb), | ||
| 754 | - v8::Global<v8::Object>(isolate(), keep_alive), | ||
| 755 | - ref); | ||
| 751 | + std::move(cb), ref); | ||
| 756 | 752 | NativeImmediateCallback* prev_tail = native_immediate_callbacks_tail_; | |
| 757 | 753 | ||
| 758 | 754 | native_immediate_callbacks_tail_ = callback.get(); | |
@@ -765,17 +761,17 @@ void Environment::CreateImmediate(Fn&& cb, | |||
| 765 | 761 | } | |
| 766 | 762 | ||
| 767 | 763 | template <typename Fn> | |
| 768 | - void Environment::SetImmediate(Fn&& cb, v8::Local<v8::Object> keep_alive) { | ||
| 769 | - CreateImmediate(std::move(cb), keep_alive, true); | ||
| 764 | + void Environment::SetImmediate(Fn&& cb) { | ||
| 765 | + CreateImmediate(std::move(cb), true); | ||
| 770 | 766 | ||
| 771 | 767 | if (immediate_info()->ref_count() == 0) | |
| 772 | 768 | ToggleImmediateRef(true); | |
| 773 | 769 | immediate_info()->ref_count_inc(1); | |
| 774 | 770 | } | |
| 775 | 771 | ||
| 776 | 772 | template <typename Fn> | |
| 777 | - void Environment::SetUnrefImmediate(Fn&& cb, v8::Local<v8::Object> keep_alive) { | ||
| 778 | - CreateImmediate(std::move(cb), keep_alive, false); | ||
| 773 | + void Environment::SetUnrefImmediate(Fn&& cb) { | ||
| 774 | + CreateImmediate(std::move(cb), false); | ||
| 779 | 775 | } | |
| 780 | 776 | ||
| 781 | 777 | Environment::NativeImmediateCallback::NativeImmediateCallback(bool refed) | |
@@ -797,10 +793,9 @@ void Environment::NativeImmediateCallback::set_next( | |||
| 797 | 793 | ||
| 798 | 794 | template <typename Fn> | |
| 799 | 795 | Environment::NativeImmediateCallbackImpl<Fn>::NativeImmediateCallbackImpl( | |
| 800 | - Fn&& callback, v8::Global<v8::Object>&& keep_alive, bool refed) | ||
| 796 | + Fn&& callback, bool refed) | ||
| 801 | 797 | : NativeImmediateCallback(refed), | |
| 802 | - callback_(std::move(callback)), | ||
| 803 | - keep_alive_(std::move(keep_alive)) {} | ||
| 798 | + callback_(std::move(callback)) {} | ||
| 804 | 799 | ||
| 805 | 800 | template <typename Fn> | |
| 806 | 801 | void Environment::NativeImmediateCallbackImpl<Fn>::Call(Environment* env) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1183,13 +1183,9 @@ class Environment : public MemoryRetainer { | |||
| 1183 | 1183 | // cb will be called as cb(env) on the next event loop iteration. | |
| 1184 | 1184 | // keep_alive will be kept alive between now and after the callback has run. | |
| 1185 | 1185 | template <typename Fn> | |
| 1186 | - inline void SetImmediate(Fn&& cb, | ||
| 1187 | - v8::Local<v8::Object> keep_alive = | ||
| 1188 | - v8::Local<v8::Object>()); | ||
| 1186 | + inline void SetImmediate(Fn&& cb); | ||
| 1189 | 1187 | template <typename Fn> | |
| 1190 | - inline void SetUnrefImmediate(Fn&& cb, | ||
| 1191 | - v8::Local<v8::Object> keep_alive = | ||
| 1192 | - v8::Local<v8::Object>()); | ||
| 1188 | + inline void SetUnrefImmediate(Fn&& cb); | ||
| 1193 | 1189 | // This needs to be available for the JS-land setImmediate(). | |
| 1194 | 1190 | void ToggleImmediateRef(bool ref); | |
| 1195 | 1191 | ||
@@ -1260,9 +1256,7 @@ class Environment : public MemoryRetainer { | |||
| 1260 | 1256 | ||
| 1261 | 1257 | private: | |
| 1262 | 1258 | template <typename Fn> | |
| 1263 | - inline void CreateImmediate(Fn&& cb, | ||
| 1264 | - v8::Local<v8::Object> keep_alive, | ||
| 1265 | - bool ref); | ||
| 1259 | + inline void CreateImmediate(Fn&& cb, bool ref); | ||
| 1266 | 1260 | ||
| 1267 | 1261 | inline void ThrowError(v8::Local<v8::Value> (*fun)(v8::Local<v8::String>), | |
| 1268 | 1262 | const char* errmsg); | |
@@ -1410,14 +1404,11 @@ class Environment : public MemoryRetainer { | |||
| 1410 | 1404 | template <typename Fn> | |
| 1411 | 1405 | class NativeImmediateCallbackImpl final : public NativeImmediateCallback { | |
| 1412 | 1406 | public: | |
| 1413 | - NativeImmediateCallbackImpl(Fn&& callback, | ||
| 1414 | - v8::Global<v8::Object>&& keep_alive, | ||
| 1415 | - bool refed); | ||
| 1407 | + NativeImmediateCallbackImpl(Fn&& callback, bool refed); | ||
| 1416 | 1408 | void Call(Environment* env) override; | |
| 1417 | 1409 | ||
| 1418 | 1410 | private: | |
| 1419 | 1411 | Fn callback_; | |
| 1420 | - v8::Global<v8::Object> keep_alive_; | ||
| 1421 | 1412 | }; | |
| 1422 | 1413 | ||
| 1423 | 1414 | std::unique_ptr<NativeImmediateCallback> native_immediate_callbacks_head_; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1582,7 +1582,8 @@ void Http2Session::MaybeScheduleWrite() { | |||
| 1582 | 1582 | HandleScope handle_scope(env()->isolate()); | |
| 1583 | 1583 | Debug(this, "scheduling write"); | |
| 1584 | 1584 | flags_ |= SESSION_STATE_WRITE_SCHEDULED; | |
| 1585 | - env()->SetImmediate([this](Environment* env) { | ||
| 1585 | + BaseObjectPtr<Http2Session> strong_ref{this}; | ||
| 1586 | + env()->SetImmediate([this, strong_ref](Environment* env) { | ||
| 1586 | 1587 | if (session_ == nullptr || !(flags_ & SESSION_STATE_WRITE_SCHEDULED)) { | |
| 1587 | 1588 | // This can happen e.g. when a stream was reset before this turn | |
| 1588 | 1589 | // of the event loop, in which case SendPendingData() is called early, | |
@@ -1595,7 +1596,7 @@ void Http2Session::MaybeScheduleWrite() { | |||
| 1595 | 1596 | HandleScope handle_scope(env->isolate()); | |
| 1596 | 1597 | InternalCallbackScope callback_scope(this); | |
| 1597 | 1598 | SendPendingData(); | |
| 1598 | - }, object()); | ||
| 1599 | + }); | ||
| 1599 | 1600 | } | |
| 1600 | 1601 | } | |
| 1601 | 1602 | ||
@@ -2043,7 +2044,8 @@ void Http2Stream::Destroy() { | |||
| 2043 | 2044 | ||
| 2044 | 2045 | // Wait until the start of the next loop to delete because there | |
| 2045 | 2046 | // may still be some pending operations queued for this stream. | |
| 2046 | - env()->SetImmediate([this](Environment* env) { | ||
| 2047 | + BaseObjectPtr<Http2Stream> strong_ref{this}; | ||
| 2048 | + env()->SetImmediate([this, strong_ref](Environment* env) { | ||
| 2047 | 2049 | // Free any remaining outgoing data chunks here. This should be done | |
| 2048 | 2050 | // here because it's possible for destroy to have been called while | |
| 2049 | 2051 | // we still have queued outbound writes. | |
@@ -2057,9 +2059,11 @@ void Http2Stream::Destroy() { | |||
| 2057 | 2059 | // We can destroy the stream now if there are no writes for it | |
| 2058 | 2060 | // already on the socket. Otherwise, we'll wait for the garbage collector | |
| 2059 | 2061 | // to take care of cleaning up. | |
| 2060 | - if (session() == nullptr || !session()->HasWritesOnSocketForStream(this)) | ||
| 2061 | - delete this; | ||
| 2062 | - }, object()); | ||
| 2062 | + if (session() == nullptr || !session()->HasWritesOnSocketForStream(this)) { | ||
| 2063 | + // Delete once strong_ref goes out of scope. | ||
| 2064 | + Detach(); | ||
| 2065 | + } | ||
| 2066 | + }); | ||
| 2063 | 2067 | ||
| 2064 | 2068 | statistics_.end_time = uv_hrtime(); | |
| 2065 | 2069 | session_->statistics_.stream_average_duration = | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -71,6 +71,7 @@ void StreamPipe::Unpipe() { | |||
| 71 | 71 | // Delay the JS-facing part with SetImmediate, because this might be from | |
| 72 | 72 | // inside the garbage collector, so we can’t run JS here. | |
| 73 | 73 | HandleScope handle_scope(env()->isolate()); | |
| 74 | + BaseObjectPtr<StreamPipe> strong_ref{this}; | ||
| 74 | 75 | env()->SetImmediate([this](Environment* env) { | |
| 75 | 76 | HandleScope handle_scope(env->isolate()); | |
| 76 | 77 | Context::Scope context_scope(env->context()); | |
@@ -105,7 +106,7 @@ void StreamPipe::Unpipe() { | |||
| 105 | 106 | .IsNothing()) { | |
| 106 | 107 | return; | |
| 107 | 108 | } | |
| 108 | - }, object()); | ||
| 109 | + }); | ||
| 109 | 110 | } | |
| 110 | 111 | ||
| 111 | 112 | uv_buf_t StreamPipe::ReadableListener::OnStreamAlloc(size_t suggested_size) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -316,9 +316,10 @@ void TLSWrap::EncOut() { | |||
| 316 | 316 | // its not clear if it is always correct. Not calling Done() could block | |
| 317 | 317 | // data flow, so for now continue to call Done(), just do it in the next | |
| 318 | 318 | // tick. | |
| 319 | - env()->SetImmediate([this](Environment* env) { | ||
| 319 | + BaseObjectPtr<TLSWrap> strong_ref{this}; | ||
| 320 | + env()->SetImmediate([this, strong_ref](Environment* env) { | ||
| 320 | 321 | InvokeQueued(0); | |
| 321 | - }, object()); | ||
| 322 | + }); | ||
| 322 | 323 | } | |
| 323 | 324 | } | |
| 324 | 325 | return; | |
@@ -349,9 +350,10 @@ void TLSWrap::EncOut() { | |||
| 349 | 350 | HandleScope handle_scope(env()->isolate()); | |
| 350 | 351 | ||
| 351 | 352 | // Simulate asynchronous finishing, TLS cannot handle this at the moment. | |
| 352 | - env()->SetImmediate([this](Environment* env) { | ||
| 353 | + BaseObjectPtr<TLSWrap> strong_ref{this}; | ||
| 354 | + env()->SetImmediate([this, strong_ref](Environment* env) { | ||
| 353 | 355 | OnStreamAfterWrite(nullptr, 0); | |
| 354 | - }, object()); | ||
| 356 | + }); | ||
| 355 | 357 | } | |
| 356 | 358 | } | |
| 357 | 359 | ||
@@ -718,9 +720,10 @@ int TLSWrap::DoWrite(WriteWrap* w, | |||
| 718 | 720 | StreamWriteResult res = | |
| 719 | 721 | underlying_stream()->Write(bufs, count, send_handle); | |
| 720 | 722 | if (!res.async) { | |
| 721 | - env()->SetImmediate([this](Environment* env) { | ||
| 723 | + BaseObjectPtr<TLSWrap> strong_ref{this}; | ||
| 724 | + env()->SetImmediate([this, strong_ref](Environment* env) { | ||
| 722 | 725 | OnStreamAfterWrite(current_empty_write_, 0); | |
| 723 | - }, object()); | ||
| 726 | + }); | ||
| 724 | 727 | } | |
| 725 | 728 | return 0; | |
| 726 | 729 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments