aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Draper <matthew@trebex.net>2016-07-09 02:39:44 +0930
committerMatthew Draper <matthew@trebex.net>2016-07-09 02:44:15 +0930
commit92421ee2d234cef370fdbf647a8ae902fc79f5f8 (patch)
treed5e3296d9d9088480f77bbe5b276c08501eb0125
parent9c4c2126b1506e2d26107c802a5abba1e6cba886 (diff)
parentce5f9bb2a5b869ae40352f5156d230a7779f896e (diff)
downloadrails-92421ee2d234cef370fdbf647a8ae902fc79f5f8.tar.gz
rails-92421ee2d234cef370fdbf647a8ae902fc79f5f8.tar.bz2
rails-92421ee2d234cef370fdbf647a8ae902fc79f5f8.zip
Merge pull request #25624 from tinco/actioncable_write_race
Fix race condition in websocket stream write
-rw-r--r--actioncable/CHANGELOG.md5
-rw-r--r--actioncable/lib/action_cable/connection/stream.rb9
2 files changed, 12 insertions, 2 deletions
diff --git a/actioncable/CHANGELOG.md b/actioncable/CHANGELOG.md
index 9aee32ddb4..83ec68a51e 100644
--- a/actioncable/CHANGELOG.md
+++ b/actioncable/CHANGELOG.md
@@ -1,3 +1,8 @@
+* Protect against concurrent writes to a websocket connection from
+ multiple threads; the underlying OS write is not always threadsafe.
+
+ *Tinco Andringa*
+
* Add ActiveSupport::Notifications hook to Broadcaster#broadcast
*Matthew Wear*
diff --git a/actioncable/lib/action_cable/connection/stream.rb b/actioncable/lib/action_cable/connection/stream.rb
index c250cf92fc..5695623859 100644
--- a/actioncable/lib/action_cable/connection/stream.rb
+++ b/actioncable/lib/action_cable/connection/stream.rb
@@ -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