Skip to content

Commit

Permalink
Fix race condition in TVar for stale reads, fixes #885
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Sep 20, 2020
1 parent c1114a0 commit 5eaecdc
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions lib/concurrent-ruby/concurrent/tvar.rb
Original file line number Diff line number Diff line change
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

0 comments on commit 5eaecdc

Please sign in to comment.