Skip to content
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

Fix race condition in TVar for stale reads, fixes #885 #886

Merged
merged 1 commit into from Sep 21, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 9 additions & 6 deletions lib/concurrent-ruby/concurrent/tvar.rb
Expand Up @@ -188,26 +188,29 @@ def read(tvar)
def write(tvar, value)
# Have we already written to this TVar?

unless @write_log.has_key? tvar
if @write_log.has_key? tvar
# Record the value written
@write_log[tvar] = value
else
# Try to lock the TVar

unless tvar.unsafe_lock.try_lock
# Someone else is writing to this TVar - abort
Concurrent::abort_transaction
end

# If we previously wrote to it, check the version hasn't changed
# Record the value written

@write_log[tvar] = value

# If we previously read from it, check the version hasn't changed

@read_log.each do |log_entry|
if log_entry.tvar == tvar and tvar.unsafe_version > log_entry.version
Concurrent::abort_transaction
end
end
end

# Record the value written

@write_log[tvar] = value
end

def abort
Expand Down