Skip to content

Commit 4e00ca9

Browse files
authored
fix: avoid leaking shared state across requests on error conditions (#83)
1 parent 31ea424 commit 4e00ca9

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

lib/inertia_rails/middleware.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ def initialize(app)
77
def call(env)
88
InertiaRailsRequest.new(@app, env)
99
.response
10+
ensure
11+
::InertiaRails.reset!
1012
end
1113

1214
class InertiaRailsRequest
@@ -19,8 +21,6 @@ def response
1921
status, headers, body = @app.call(@env)
2022
request = ActionDispatch::Request.new(@env)
2123

22-
::InertiaRails.reset!
23-
2424
# Inertia errors are added to the session via redirect_to
2525
request.session.delete(:inertia_errors) unless keep_inertia_errors?(status)
2626

spec/dummy/app/controllers/inertia_multithreaded_share_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ def share_multithreaded
66
sleep 1
77
render inertia: 'ShareTestComponent'
88
end
9+
10+
def share_multithreaded_error
11+
raise Exception
12+
end
913
end

spec/dummy/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
delete 'redirect_test' => 'inertia_test#redirect_test'
1818
get 'my_location' => 'inertia_test#my_location'
1919
get 'share_multithreaded' => 'inertia_multithreaded_share#share_multithreaded'
20+
get 'share_multithreaded_error' => 'inertia_multithreaded_share#share_multithreaded_error'
2021
get 'redirect_with_inertia_errors' => 'inertia_test#redirect_with_inertia_errors'
2122
post 'redirect_with_inertia_errors' => 'inertia_test#redirect_with_inertia_errors'
2223
post 'redirect_back_with_inertia_errors' => 'inertia_test#redirect_back_with_inertia_errors'

spec/inertia/sharing_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,15 @@
8888
thread1.join
8989
thread2.join
9090
end
91+
92+
it 'is expected not to leak shared data across requests' do
93+
begin
94+
get share_multithreaded_error_path, headers: {'X-Inertia' => true}
95+
rescue Exception
96+
end
97+
98+
expect(InertiaRails.shared_plain_data).to be_empty
99+
expect(InertiaRails.shared_blocks).to be_empty
100+
end
91101
end
92102
end

0 commit comments

Comments
 (0)