mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #25624 from tinco/actioncable_write_race
Fix race condition in websocket stream write
This commit is contained in:
parent
57f5df3ebf
commit
7340459cdb
2 changed files with 13 additions and 2 deletions
|
@ -1,3 +1,9 @@
|
|||
* Protect against concurrent writes to a websocket connection from
|
||||
multiple threads; the underlying OS write is not always threadsafe.
|
||||
|
||||
*Tinco Andringa*
|
||||
|
||||
|
||||
## Rails 5.0.0 (June 30, 2016) ##
|
||||
|
||||
* Fix development reloading support: new cable connections are now correctly
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
require 'thread'
|
||||
|
||||
module ActionCable
|
||||
module Connection
|
||||
#--
|
||||
|
@ -11,6 +13,7 @@ module ActionCable
|
|||
@stream_send = socket.env['stream.send']
|
||||
|
||||
@rack_hijack_io = nil
|
||||
@write_lock = Mutex.new
|
||||
end
|
||||
|
||||
def each(&callback)
|
||||
|
@ -27,8 +30,10 @@ module ActionCable
|
|||
end
|
||||
|
||||
def write(data)
|
||||
return @rack_hijack_io.write(data) if @rack_hijack_io
|
||||
return @stream_send.call(data) if @stream_send
|
||||
@write_lock.synchronize do
|
||||
return @rack_hijack_io.write(data) if @rack_hijack_io
|
||||
return @stream_send.call(data) if @stream_send
|
||||
end
|
||||
rescue EOFError, Errno::ECONNRESET
|
||||
@socket_object.client_gone
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue