-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically close any open writable connections after a fork.
- Loading branch information
1 parent
f1d4bce
commit c6bc510
Showing
7 changed files
with
196 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
require "weakref" | ||
|
||
# based on Rails's active_support/fork_safety.rb | ||
module SQLite3 | ||
module ForkSafety | ||
module CoreExt | ||
def _fork | ||
pid = super | ||
if pid == 0 | ||
ForkSafety.discard | ||
end | ||
pid | ||
end | ||
end | ||
|
||
@databases = [] | ||
|
||
class << self | ||
def hook! | ||
::Process.singleton_class.prepend(CoreExt) | ||
end | ||
|
||
def track(database) | ||
@databases << WeakRef.new(database) | ||
end | ||
|
||
def discard | ||
@databases.each do |db| | ||
next unless db.weakref_alive? | ||
|
||
unless db.closed? || db.readonly? | ||
db.close | ||
end | ||
end | ||
@databases.clear | ||
end | ||
end | ||
end | ||
end | ||
|
||
SQLite3::ForkSafety.hook! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters