From d8dc8608e58ed43b406c32ee0686fa5bcdcfa85f Mon Sep 17 00:00:00 2001 From: Mark Li Date: Mon, 9 Sep 2019 14:37:30 -0700 Subject: [PATCH 1/4] Fix locking with unsaved changes bug --- .../devise_token_auth/concerns/set_user_by_token.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/controllers/devise_token_auth/concerns/set_user_by_token.rb b/app/controllers/devise_token_auth/concerns/set_user_by_token.rb index 027a9261a..9b36a52e9 100644 --- a/app/controllers/devise_token_auth/concerns/set_user_by_token.rb +++ b/app/controllers/devise_token_auth/concerns/set_user_by_token.rb @@ -116,6 +116,13 @@ def update_auth_header private def refresh_headers + # At this point restore the attributes so we can obtain the lock + # otherwise an exception is thrown while trying to obtain the lock + # with unsaved data on the resource. + unless const_defined?('Mongoid') do + @resource.restore_attributes + end + # Lock the user record during any auth_header updates to ensure # we don't have write contention from multiple threads @resource.with_lock do From 334813b766406e4cb9ef71916e338661045350d8 Mon Sep 17 00:00:00 2001 From: Mark Li Date: Thu, 12 Sep 2019 15:08:53 -0700 Subject: [PATCH 2/4] Remove do --- app/controllers/devise_token_auth/concerns/set_user_by_token.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/devise_token_auth/concerns/set_user_by_token.rb b/app/controllers/devise_token_auth/concerns/set_user_by_token.rb index 9b36a52e9..c14195a53 100644 --- a/app/controllers/devise_token_auth/concerns/set_user_by_token.rb +++ b/app/controllers/devise_token_auth/concerns/set_user_by_token.rb @@ -119,7 +119,7 @@ def refresh_headers # At this point restore the attributes so we can obtain the lock # otherwise an exception is thrown while trying to obtain the lock # with unsaved data on the resource. - unless const_defined?('Mongoid') do + unless const_defined?('Mongoid') @resource.restore_attributes end From b458903b9b3c1ea14a05283d7a4783aef0b5247a Mon Sep 17 00:00:00 2001 From: Mark Li Date: Thu, 12 Sep 2019 15:43:11 -0700 Subject: [PATCH 3/4] Refactor --- .../devise_token_auth/concerns/set_user_by_token.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/controllers/devise_token_auth/concerns/set_user_by_token.rb b/app/controllers/devise_token_auth/concerns/set_user_by_token.rb index c14195a53..af2812369 100644 --- a/app/controllers/devise_token_auth/concerns/set_user_by_token.rb +++ b/app/controllers/devise_token_auth/concerns/set_user_by_token.rb @@ -119,9 +119,7 @@ def refresh_headers # At this point restore the attributes so we can obtain the lock # otherwise an exception is thrown while trying to obtain the lock # with unsaved data on the resource. - unless const_defined?('Mongoid') - @resource.restore_attributes - end + @resource.restore_attributes unless const_defined?('Mongoid') # Lock the user record during any auth_header updates to ensure # we don't have write contention from multiple threads From 8d8417584001bd1ce4ee773374c2d1f1bbfa179c Mon Sep 17 00:00:00 2001 From: Mark Li Date: Tue, 29 Oct 2019 15:29:27 -0700 Subject: [PATCH 4/4] WIP --- .../devise_token_auth/concerns/set_user_by_token.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/devise_token_auth/concerns/set_user_by_token.rb b/app/controllers/devise_token_auth/concerns/set_user_by_token.rb index af2812369..60bd331bf 100644 --- a/app/controllers/devise_token_auth/concerns/set_user_by_token.rb +++ b/app/controllers/devise_token_auth/concerns/set_user_by_token.rb @@ -119,8 +119,8 @@ def refresh_headers # At this point restore the attributes so we can obtain the lock # otherwise an exception is thrown while trying to obtain the lock # with unsaved data on the resource. - @resource.restore_attributes unless const_defined?('Mongoid') - + @resource.restore_attributes unless defined?('Mongoid') + # Lock the user record during any auth_header updates to ensure # we don't have write contention from multiple threads @resource.with_lock do