123456789_123456789_123456789_123456789_123456789_

Module: SQLite3::ForkSafety

Relationships & Source Files
Namespace Children
Modules:
Defined in: lib/sqlite3/fork_safety.rb

Overview

based on Rails’s active_support/fork_tracker.rb

Class Method Summary

Class Method Details

.discard

This method is for internal use only.
[ GitHub ]

  
# File 'lib/sqlite3/fork_safety.rb', line 33

def discard # :nodoc:
  warned = @suppress
  @databases.each do |db|
    next unless db.weakref_alive?

    begin
      unless db.closed? || db.readonly?
        unless warned
          # If you are here, you may want to read
          # https://github.com/sparklemotion/sqlite3-ruby/pull/558
          warn("Writable sqlite database connection(s) were inherited from a forked process. " \
               "This is unsafe and the connections are being closed to prevent possible data " \
               "corruption. Please close writable sqlite database connections before forking.",
            uplevel: 0)
          warned = true
        end
        db.close
      end
    rescue WeakRef::RefError
      # GC may run while this method is executing, and that's OK
    end
  end
  @databases.clear
end

.hook!

This method is for internal use only.
[ GitHub ]

  
# File 'lib/sqlite3/fork_safety.rb', line 23

def hook! # :nodoc:
  ::Process.singleton_class.prepend(CoreExt)
end

.suppress_warnings!

Call to suppress the fork-related warnings.

[ GitHub ]

  
# File 'lib/sqlite3/fork_safety.rb', line 59

def suppress_warnings!
  @suppress = true
end

.track(database)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/sqlite3/fork_safety.rb', line 27

def track(database) # :nodoc:
  @mutex.synchronize do
    @databases << WeakRef.new(database)
  end
end