Skip to content

Commit 7340459

Browse files
committed
Merge pull request rails#25624 from tinco/actioncable_write_race
Fix race condition in websocket stream write
1 parent 57f5df3 commit 7340459

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

actioncable/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Protect against concurrent writes to a websocket connection from
2+
multiple threads; the underlying OS write is not always threadsafe.
3+
4+
*Tinco Andringa*
5+
6+
17
## Rails 5.0.0 (June 30, 2016) ##
28

39
* Fix development reloading support: new cable connections are now correctly

actioncable/lib/action_cable/connection/stream.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'thread'
2+
13
module ActionCable
24
module Connection
35
#--
@@ -11,6 +13,7 @@ def initialize(event_loop, socket)
1113
@stream_send = socket.env['stream.send']
1214

1315
@rack_hijack_io = nil
16+
@write_lock = Mutex.new
1417
end
1518

1619
def each(&callback)
@@ -27,8 +30,10 @@ def shutdown
2730
end
2831

2932
def write(data)
30-
return @rack_hijack_io.write(data) if @rack_hijack_io
31-
return @stream_send.call(data) if @stream_send
33+
@write_lock.synchronize do
34+
return @rack_hijack_io.write(data) if @rack_hijack_io
35+
return @stream_send.call(data) if @stream_send
36+
end
3237
rescue EOFError, Errno::ECONNRESET
3338
@socket_object.client_gone
3439
end

0 commit comments

Comments
 (0)