Skip to content
Browse files

KNOWN_ISSUES: background threads + Logger monkey patch

Thanks to Michael Guterl for informing us of the issues
and testing the monkey patch.
  • Loading branch information...
1 parent cb6d8c7 commit 813cdd3cbc35a86ebf33aaf028937e70a15ddba0 Eric Wong committed
Showing with 33 additions and 0 deletions.
  1. +8 −0 KNOWN_ISSUES
  2. +25 −0 examples/logger_mp_safe.rb
@@ -3,6 +3,14 @@
Occasionally odd {issues}[link:ISSUES.html] arise without a transparent or
acceptable solution. Those issues are documented here.
+* When using "preload_app true", with apps using background threads
+ need to restart them in the after_fork hook because threads are never
+ shared with child processes. Additionally, any synchronization
+ primitives (Mutexes, Monitors, ConditionVariables) should be
+ reinitialized in case they are held during fork time to avoid
+ deadlocks. The core Ruby Logger class needlessly uses a MonitorMutex
+ which can be disabled with a {monkey patch}[link:examples/logger_mp_safe.rb]
* Rails 2.3.2 bundles its own version of Rack. This may cause subtle
bugs when simultaneously loaded with the system-wide Rack Rubygem
which Unicorn depends on. Upgrading to Rails 2.3.4 (or later) is
25 examples/logger_mp_safe.rb
@@ -0,0 +1,25 @@
+# Multi-Processing-safe monkey patch for Logger
+# This monkey patch fixes the case where "preload_app true" is used and
+# the application spawns a background thread upon being loaded.
+# This removes all lock from the Logger code and solely relies on the
+# underlying filesystem to handle write(2) system calls atomically when
+# O_APPEND is used. This is safe in the presence of both multiple
+# threads (native or green) and multiple processes when writing to
+# a filesystem with POSIX O_APPEND semantics.
+# It should be noted that the original locking on Logger could _never_ be
+# considered reliable on non-POSIX filesystems with multiple processes,
+# either, so nothing is lost in that case.
+require 'logger'
+class Logger::LogDevice
+ def write(message)
+ @dev.syswrite(message)
+ end
+ def close
+ @dev.close
+ end

0 comments on commit 813cdd3

Please sign in to comment.
Something went wrong with that request. Please try again.