Module: SemanticLogger::RactorReceiver
- Defined in:
- lib/semantic_logger/ractor_receiver.rb
Overview
Internal, experimental: in the main Ractor, receives the log entries logged in other Ractors and hands them to the processor, which writes them to the appenders. Ruby 4.0 and later only.
It knows the port that the other Ractors send to (see RactorProcessor), and runs the thread in the main Ractor that receives from it. The thread starts with the processor's thread, when an appender is added. Entries sent before then wait on the port.
Ruby 4.0 can crash, or hang, a forked child process that uses a port, when a thread of the parent was waiting on a port at the time of the fork. So no thread of the main Ractor other than the receiving thread ever waits on a port: a thread that waits for the receiving thread, such as one flushing, waits on a Queue instead. And the receiving thread itself waits on a Queue while the process forks (see .pause).
A thread that waits for the receiving thread hands it that Queue without locking a Mutex, so that it can do so in a signal trap, such as one that forks or flushes, where Ruby does not allow one to be locked.
Constant Summary collapse
- PAUSE_TIMEOUT =
Seconds that .pause waits for the receiving thread to finish the entry it is handing over.
5- PAUSE_CHECK_SECONDS =
Seconds that .pause waits at a time for the receiving thread to get to the pause, before it checks again. Ruby does not end a wait on a Queue that a signal trap interrupted when the Queue is closed while the trap runs, until the wait times out; the receiving thread does that when it gets to a pause that a pause in the trap shares.
0.1- PAUSING =
The thread variable that holds, while a thread pauses the receiving thread, the Queue that the receiving thread closes once it waits for the fork to be over (see .pause).
:semantic_logger_pausing_ractor_receiver
Class Attribute Summary collapse
-
.port ⇒ Object
readonly
Returns [Ractor::Port] where other Ractors send their log entries and requests.
Class Method Summary collapse
-
.flush ⇒ Object
Waits until every entry sent to the port before this call has been handed to the processor, then flushes the processor.
-
.pause ⇒ Object
Runs the block, which forks the process, while the receiving thread waits on a Queue rather than on the port.
-
.reopen ⇒ Object
Restarts the receiving thread, if it was started: in the child process after a fork, which the thread does not survive, or after it stopped.
-
.running? ⇒ Boolean
Returns [true|false] whether a receiving thread runs in this process.
-
.start ⇒ Object
Starts the receiving thread, unless it is running.
Class Attribute Details
.port ⇒ Object (readonly)
Returns [Ractor::Port] where other Ractors send their log entries and requests. Any Ractor can read it, since a port is shareable.
50 51 52 |
# File 'lib/semantic_logger/ractor_receiver.rb', line 50 def port @port end |
Class Method Details
.flush ⇒ Object
Waits until every entry sent to the port before this call has been handed to the processor, then flushes the processor. Returns the result of the processor's flush.
It flushes the processor without waiting on a thread that pauses the receiving thread (see .pause), such as one that flushes while it forks: the receiving thread has handed over the entries sent before the fork began, and waits until it is over. And on a thread inside the processor, such as in a signal trap that interrupted a log call in synchronous mode: the receiving thread would wait for that call to return before it could hand over another entry.
Takes no lock, so that it can be called in a signal trap.
139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/semantic_logger/ractor_receiver.rb', line 139 def flush thread = @thread return Logger.processor.flush unless waits_for_receiving_thread?(thread) done = Queue.new @port << [:flush, register(done)] loop do reply = done.pop(timeout: 1) return reply.first if reply return Logger.processor.flush unless thread.alive? end end |
.pause ⇒ Object
Runs the block, which forks the process, while the receiving thread waits on a Queue rather than on the port. Returns the result of the block.
The receiving thread first finishes handing over the entries sent before, waiting at most PAUSE_TIMEOUT seconds, after which the fork goes ahead anyway.
A pause in a signal trap that interrupted another one on the same thread shares it, rather than asking the receiving thread to pause again: it would only get to the request once the other pause is over, which cannot be before the trap is.
Takes no lock, so that it can be called in a signal trap, such as one that forks.
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/semantic_logger/ractor_receiver.rb', line 115 def pause(&) thread = @thread unless RactorSupport.other_ractor? return yield unless thread&.alive? && !Thread.current.equal?(thread) paused = Thread.current.thread_variable_get(PAUSING) if paused wait_until_paused(paused) # Unless the other pause ended meanwhile, which another Fiber of this thread can do. return yield if Thread.current.thread_variable_get(PAUSING).equal?(paused) end hold_receiving_thread(&) end |
.reopen ⇒ Object
Restarts the receiving thread, if it was started: in the child process after a fork, which the thread does not survive, or after it stopped. The child also gets a new port, since the port it inherited still holds the entries sent to the parent before the fork, which the parent writes.
Also called from a signal trap, such as one that reopens the log files after a rotation, where Ruby does not allow a Mutex to be locked. So within the same process, a receiving thread that runs, or none that was started, is left as it is without taking the lock. When there is more to do, and Ruby refuses the lock, a new thread, which can take it, does it instead, once this call has returned.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/semantic_logger/ractor_receiver.rb', line 84 def reopen return if @pid == ::Process.pid && (!@started || @thread&.alive?) locked = TrapSafe.try_synchronize(@mutex) do unless @pid == ::Process.pid @port = ::Ractor::Port.new @pid = ::Process.pid @thread = nil @waits = Queue.new @registered = {} end end if locked start if @started else Thread.new { reopen } nil end end |
.running? ⇒ Boolean
Returns [true|false] whether a receiving thread runs in this process. Any Ractor can read it.
53 54 55 |
# File 'lib/semantic_logger/ractor_receiver.rb', line 53 def running? @running == ::Process.pid end |
.start ⇒ Object
Starts the receiving thread, unless it is running.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/semantic_logger/ractor_receiver.rb', line 58 def start @mutex.synchronize do return false if @thread&.alive? port = @port @started = true @running = ::Process.pid @thread = Thread.new do Thread.current.name = "SL Ractors" receive(port) ensure @running = nil end end true end |