From ab48cb54cab10f5b4e46305fb5acfaf346a1178f Mon Sep 17 00:00:00 2001 From: Stephen von Takach Date: Thu, 17 May 2018 01:47:23 +1000 Subject: [PATCH] (libuv:reactor) replace default threadpool to prevent internal libuv tasks from being blocked by application tasks --- lib/libuv/reactor.rb | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/libuv/reactor.rb b/lib/libuv/reactor.rb index 6c175bd..237af0d 100644 --- a/lib/libuv/reactor.rb +++ b/lib/libuv/reactor.rb @@ -9,6 +9,7 @@ class Reactor CRITICAL = ::Mutex.new + THREAD_POOL = ::Concurrent::FixedThreadPool.new(8) module ClassMethods @@ -425,9 +426,19 @@ def on_program_interrupt(&callback) # @param callback [Proc] the callback to be called in the thread pool # @return [::Libuv::Work] # @raise [ArgumentError] if block is not given - def work(&callback) - assert_block(callback) - Work.new(@reactor, &callback) + def work + ref + d = defer + THREAD_POOL.post do + begin + d.resolve(yield) + rescue Exception => e + d.reject(e) + end + end + promise = d.promise + promise.finally { unref } + promise end # Lookup a hostname