-
Notifications
You must be signed in to change notification settings - Fork 220
Initial Ractor support #365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sandlerr
wants to merge
11
commits into
sparklemotion:main
Choose a base branch
from
sandlerr:sandlerr/ractor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
958dbf4
Initial Ractor support
sandlerr 6ccb27b
test: handle Ruby 3.3 behavior around passing unshareable objects
flavorjones 093c6cc
Merge branch 'main' into sandlerr/ractor
tenderlove 2b0fff5
Allow databases to be shareable but only in full-mutex mode
tenderlove 36af661
Merge branch 'main' into sandlerr/ractor
tenderlove 46fcc42
Fix cop?
tenderlove 9e64a7a
remove dead code
tenderlove c7a24ba
not all versions of Ruby have Ractor.main?
tenderlove 90dec0a
Check for Ractor constant
tenderlove 0d2ee96
more TruffleRuby
tenderlove 433c2d3
more truffle ruby
tenderlove File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,121 @@ | ||
| # Ractor support in SQLite3-Ruby | ||
|
|
||
| SQLite3 has [3 different modes of threading support](https://www.sqlite.org/threadsafe.html). | ||
|
|
||
| 1. Single-thread | ||
| 2. Multi-thread | ||
| 3. Serialized | ||
|
|
||
| "Single thread" mode means there are no mutexes, so the library itself is not | ||
| thread safe. In other words if two threads do `SQLite3::Database.new` on the | ||
| same file, it will have thread safety problems. | ||
|
|
||
| "Multi thread" mode means that SQLite3 puts mutexes in place, but it does not | ||
| mean that the SQLite3 API itself is thread safe. In other words, in this mode | ||
| it is SAFE for two threads to do `SQLite3::Database.new` on the same file, but | ||
| it is NOT SAFE to share that database object between two threads. | ||
|
|
||
| "Serialized" mode is like "Multi thread" mode except that there are mutexes in | ||
| place such that it IS SAFE to share the same database object between threads. | ||
|
|
||
| ## Ractor Safety | ||
|
|
||
| When a C extension claims to be Ractor safe by calling `rb_ext_ractor_safe`, | ||
| it's merely claiming that it's C API is thread safe. This _does not_ mean that | ||
| objects allocated from said C extension are allowed to cross between Ractor | ||
| boundaries. | ||
|
|
||
| In other words, `rb_ext_ractor_safe` matches the expectations of the | ||
| "multi-thread" mode of SQLite3. We can detect the multithread mode via the | ||
| `sqlite3_threadsafe` function. In other words, it's fine to declare this | ||
| extension is Ractor safe, but only if `sqlite3_threadsafe` returns true. | ||
|
|
||
| Even if we call `rb_ext_ractor_safe`, no database objects are allowed to be | ||
| passed between Ractors. For example, this code will break with a Ractor error: | ||
|
|
||
| ```ruby | ||
| require "sqlite3" | ||
|
|
||
| r = Ractor.new { | ||
| loop do | ||
| break unless Ractor.receive | ||
| end | ||
| } | ||
|
|
||
| db = SQLite3::Database.new ":memory:" | ||
|
|
||
| begin | ||
| r.send db | ||
| puts "unreachable" | ||
| rescue Ractor::Error | ||
| end | ||
| ``` | ||
|
|
||
| If the user opens the database in "Serialized" mode, then it _is_ OK to pass | ||
| the database object between Ractors, or access the database in parallel because | ||
| the SQLite API is fully thread-safe. | ||
|
|
||
| Passing the db connection is fine: | ||
|
|
||
| ```ruby | ||
| r = Ractor.new { | ||
| loop do | ||
| break unless Ractor.receive | ||
| end | ||
| } | ||
|
|
||
| db = SQLite3::Database.new ":memory:", | ||
| flags: SQLite3::Constants::Open::FULLMUTEX | | ||
| SQLite3::Constants::Open::READWRITE | | ||
| SQLite3::Constants::Open::CREATE | ||
|
|
||
| # works | ||
| r.send db | ||
| ``` | ||
|
|
||
| Access the DB connection via global is fine: | ||
|
|
||
| ```ruby | ||
| require "sqlite3" | ||
|
|
||
| DB = SQLite3::Database.new ":memory:", | ||
| flags: SQLite3::Constants::Open::FULLMUTEX | | ||
| SQLite3::Constants::Open::READWRITE | | ||
| SQLite3::Constants::Open::CREATE | ||
|
|
||
| r = Ractor.new { | ||
| loop do | ||
| Ractor.receive | ||
| p DB | ||
| end | ||
| } | ||
|
|
||
|
|
||
| r.send 123 | ||
| sleep | ||
| ``` | ||
|
|
||
| ## Fork Safety | ||
|
|
||
| Fork safety is restricted to database objects that were created on the main | ||
| Ractor. When a process forks, the child process shuts down all Ractors, so | ||
| any database connections that are inside a Ractor should be released. | ||
|
|
||
| However, this doesn't account for a situation where a child Ractor passes a | ||
| database to the main Ractor: | ||
|
|
||
| ```ruby | ||
| require "sqlite3" | ||
|
|
||
| db = Ractor.new { | ||
| SQLite3::Database.new ":memory:", | ||
| flags: SQLite3::Constants::Open::FULLMUTEX | | ||
| SQLite3::Constants::Open::READWRITE | | ||
| SQLite3::Constants::Open::CREATE | ||
| }.take | ||
|
|
||
| fork { | ||
| # db wasn't tracked | ||
| p db | ||
| } | ||
| ``` |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,52 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "helper" | ||
| require "fileutils" | ||
|
|
||
| class IntegrationRactorTestCase < SQLite3::TestCase | ||
| def test_ractor_safe | ||
| skip unless RUBY_VERSION >= "3.0" && SQLite3.threadsafe? | ||
| skip unless defined?(Ractor) | ||
| assert_predicate SQLite3, :ractor_safe? | ||
| end | ||
|
|
||
| def test_ractor_share_database | ||
| skip("Requires Ruby with Ractors") unless SQLite3.ractor_safe? | ||
|
|
||
| db = SQLite3::Database.open(":memory:") | ||
|
|
||
| if RUBY_VERSION >= "3.3" | ||
| # after ruby/ruby@ce47ee00 | ||
| ractor = Ractor.new do | ||
| Ractor.receive | ||
| end | ||
|
|
||
| assert_raises(Ractor::Error) { ractor.send(db) } | ||
| else | ||
| # before ruby/ruby@ce47ee00 T_DATA objects could be copied | ||
| ractor = Ractor.new do | ||
| local_db = Ractor.receive | ||
| Ractor.yield local_db.object_id | ||
| end | ||
| ractor.send(db) | ||
| copy_id = ractor.take | ||
|
|
||
| assert_not_equal db.object_id, copy_id | ||
| end | ||
| end | ||
|
|
||
| def test_shareable_db | ||
| skip unless defined?(Ractor) | ||
|
|
||
| # databases are shareable between ractors, but only if they're opened | ||
| # in "full mutex" mode | ||
| db = SQLite3::Database.new ":memory:", | ||
| flags: SQLite3::Constants::Open::FULLMUTEX | | ||
| SQLite3::Constants::Open::READWRITE | | ||
| SQLite3::Constants::Open::CREATE | ||
| assert Ractor.shareable?(db) | ||
|
|
||
| db = SQLite3::Database.new ":memory:" | ||
| refute Ractor.shareable?(db) | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HAVE_RB_EXT_RACTOR_SAFEis defined directly by Ruby headers so there is no need for thishave_func: https://github.com/ruby/ruby/blob/e77b7b6f1e3759c849d0bd11106e8acaaac0a725/include/ruby/internal/intern/load.h#L239-L249