Maintenance: Fix unhandled race condidtions in file locking of the web socket server.

This commit is contained in:
Martin Gruner 2021-09-16 14:33:28 +02:00
parent 2d31b6ced4
commit 770c1271a5

View file

@ -70,11 +70,7 @@ class Sessions::Store::File
def set(client_id, data)
path = "#{@path}/#{client_id}"
File.open("#{path}/session", 'wb') do |file|
file.flock(File::LOCK_EX)
file.write data.to_json
file.flock(File::LOCK_UN)
end
write_with_lock("#{path}/session", data.to_json)
end
def get(client_id)
@ -85,16 +81,11 @@ class Sessions::Store::File
return if !check_session_file_for_client(client_id, session_dir, session_file)
begin
File.open(session_file, 'rb') do |file|
file.flock(File::LOCK_SH)
all = file.read
file.flock(File::LOCK_UN)
data_json = JSON.parse(all)
data_json = JSON.parse(read_with_lock(session_file))
if data_json
data = Sessions.symbolize_keys(data_json)
data[:user] = data_json['user'] # for compat. reasons
end
end
rescue => e
Sessions.log('error', e.inspect)
destroy(client_id)
@ -109,12 +100,7 @@ class Sessions::Store::File
return false if !location
begin
File.open(location, 'wb') do |file|
file.flock(File::LOCK_EX)
file.write data.to_json
file.flock(File::LOCK_UN)
file.close
end
write_with_lock(location, data.to_json)
rescue => e
Sessions.log('error', e.inspect)
Sessions.log('error', "error in writing message file '#{location}'")
@ -156,11 +142,7 @@ class Sessions::Store::File
FileUtils.mkpath path
file_path = "#{path}/#{Time.now.utc.to_f}-#{rand(99_999)}"
File.open(file_path, 'wb') do |file|
file.flock(File::LOCK_EX)
file.write data.to_json
file.flock(File::LOCK_UN)
end
write_with_lock(file_path, data.to_json)
end
def each_spool()
@ -178,15 +160,10 @@ class Sessions::Store::File
filename = "#{path}/#{entry}"
next if !File.exist?(filename)
File.open(filename, 'rb') do |file|
file.flock(File::LOCK_SH)
message = file.read
file.flock(File::LOCK_UN)
message = read_with_lock(filename)
yield message, entry
end
end
end
def remove_from_spool(_message, entry)
path = "#{@path}/spool/"
@ -209,11 +186,8 @@ class Sessions::Store::File
nodes = []
files = Dir.glob(path)
files.each do |filename|
File.open(filename, 'rb') do |file|
file.flock(File::LOCK_SH)
content = file.read
file.flock(File::LOCK_UN)
begin
content = read_with_lock(filename)
data = JSON.parse(content)
nodes.push data
rescue => e
@ -222,7 +196,6 @@ class Sessions::Store::File
# next
end
end
end
nodes
end
@ -236,9 +209,7 @@ class Sessions::Store::File
content = data.to_json
# store session data in session file
File.open(status_file, 'wb') do |file|
file.write content
end
write_with_lock(status_file, content)
end
def each_node_session()
@ -247,11 +218,8 @@ class Sessions::Store::File
files = Dir.glob(path)
files.each do |filename|
File.open(filename, 'rb') do |file|
file.flock(File::LOCK_SH)
content = file.read
file.flock(File::LOCK_UN)
begin
content = read_with_lock(filename)
next if content.blank?
data = JSON.parse(content)
@ -265,7 +233,6 @@ class Sessions::Store::File
end
end
end
end
def create_node_session(node_id, client_id, data)
if !File.exist?(@nodes_path)
@ -276,9 +243,7 @@ class Sessions::Store::File
content = data.to_json
# store session data in session file
File.open(status_file, 'wb') do |file|
file.write content
end
write_with_lock(status_file, content)
end
def each_session_by_node(node_id)
@ -287,11 +252,8 @@ class Sessions::Store::File
files = Dir.glob(path)
files.each do |filename|
File.open(filename, 'rb') do |file|
file.flock(File::LOCK_SH)
content = file.read
file.flock(File::LOCK_UN)
begin
content = read_with_lock(filename)
next if content.blank?
data = JSON.parse(content)
@ -305,10 +267,24 @@ class Sessions::Store::File
end
end
end
end
private
def write_with_lock(filename, data)
File.open(filename, 'ab') do |file|
file.flock(File::LOCK_EX)
file.truncate 0 # Truncate only after locking to avoid empty state
file.write data
end
end
def read_with_lock(filename)
File.open(filename, 'rb') do |file|
file.flock(File::LOCK_SH)
return file.read
end
end
def queue_file_read(path, filename)
location = "#{path}#{filename}"
message = ''