diff --git a/lib/sqlite3/fork_safety.rb b/lib/sqlite3/fork_safety.rb index 79eb8b52..a1bc175a 100644 --- a/lib/sqlite3/fork_safety.rb +++ b/lib/sqlite3/fork_safety.rb @@ -35,17 +35,21 @@ def discard # :nodoc: @databases.each do |db| next unless db.weakref_alive? - 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 + 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 - db.close + rescue WeakRef::RefError + # GC may run while this method is executing, and that's OK end end @databases.clear