Skip to content

Commit 5eaecdc

Browse files
committed
Fix race condition in TVar for stale reads, fixes #885
1 parent c1114a0 commit 5eaecdc

File tree

1 file changed

+9
-6
lines changed
  • lib/concurrent-ruby/concurrent

1 file changed

+9
-6
lines changed

lib/concurrent-ruby/concurrent/tvar.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,26 +188,29 @@ def read(tvar)
188188
def write(tvar, value)
189189
# Have we already written to this TVar?
190190

191-
unless @write_log.has_key? tvar
191+
if @write_log.has_key? tvar
192+
# Record the value written
193+
@write_log[tvar] = value
194+
else
192195
# Try to lock the TVar
193196

194197
unless tvar.unsafe_lock.try_lock
195198
# Someone else is writing to this TVar - abort
196199
Concurrent::abort_transaction
197200
end
198201

199-
# If we previously wrote to it, check the version hasn't changed
202+
# Record the value written
203+
204+
@write_log[tvar] = value
205+
206+
# If we previously read from it, check the version hasn't changed
200207

201208
@read_log.each do |log_entry|
202209
if log_entry.tvar == tvar and tvar.unsafe_version > log_entry.version
203210
Concurrent::abort_transaction
204211
end
205212
end
206213
end
207-
208-
# Record the value written
209-
210-
@write_log[tvar] = value
211214
end
212215

213216
def abort

0 commit comments

Comments
 (0)