BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gitlab-ce
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
gitlab-ce
Commits
725b3837
Commit
725b3837
authored
Aug 14, 2017
by
Dmitriy Zaporozhets
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'fix/thread-safe-gpgme-tmp-directory' into 'master'
Fix: Thread safe GPGME tmp directory Closes #35986 See merge request !13481
parents
56054c3f
a1759666
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
85 additions
and
11 deletions
+85
-11
fix-thread-safe-gpgme-tmp-directory.yml
...gelogs/unreleased/fix-thread-safe-gpgme-tmp-directory.yml
+4
-0
gpg.rb
lib/gitlab/gpg.rb
+29
-11
gpg_spec.rb
spec/lib/gitlab/gpg_spec.rb
+52
-0
No files found.
changelogs/unreleased/fix-thread-safe-gpgme-tmp-directory.yml
0 → 100644
View file @
725b3837
---
title
:
Make GPGME temporary directory handling thread safe
merge_request
:
13481
author
:
Alexis Reigel
lib/gitlab/gpg.rb
View file @
725b3837
...
...
@@ -2,6 +2,8 @@ module Gitlab
module
Gpg
extend
self
MUTEX
=
Mutex
.
new
module
CurrentKeyChain
extend
self
...
...
@@ -42,21 +44,37 @@ module Gitlab
end
end
def
using_tmp_keychain
Dir
.
mktmpdir
do
|
dir
|
@original_dirs
||=
[
GPGME
::
Engine
.
dirinfo
(
'homedir'
)]
@original_dirs
.
push
(
dir
)
GPGME
::
Engine
.
home_dir
=
dir
return_value
=
yield
# Allows thread safe switching of temporary keychain files
#
# 1. The current thread may use nesting of temporary keychain
# 2. Another thread needs to wait for the lock to be released
def
using_tmp_keychain
(
&
block
)
if
MUTEX
.
locked?
&&
MUTEX
.
owned?
optimistic_using_tmp_keychain
(
&
block
)
else
MUTEX
.
synchronize
do
optimistic_using_tmp_keychain
(
&
block
)
end
end
end
@original_dirs
.
pop
# 1. Returns the custom home directory if one has been set by calling
# `GPGME::Engine.home_dir=`
# 2. Returns the default home directory otherwise
def
current_home_dir
GPGME
::
Engine
.
info
.
first
.
home_dir
||
GPGME
::
Engine
.
dirinfo
(
'homedir'
)
end
GPGME
::
Engine
.
home_dir
=
@original_dirs
[
-
1
]
private
return_value
def
optimistic_using_tmp_keychain
previous_dir
=
current_home_dir
Dir
.
mktmpdir
do
|
dir
|
GPGME
::
Engine
.
home_dir
=
dir
yield
end
ensure
GPGME
::
Engine
.
home_dir
=
previous_dir
end
end
end
spec/lib/gitlab/gpg_spec.rb
View file @
725b3837
...
...
@@ -43,6 +43,58 @@ describe Gitlab::Gpg do
).
to
eq
[]
end
end
describe
'.current_home_dir'
do
let
(
:default_home_dir
)
{
GPGME
::
Engine
.
dirinfo
(
'homedir'
)
}
it
'returns the default value when no explicit home dir has been set'
do
expect
(
described_class
.
current_home_dir
).
to
eq
default_home_dir
end
it
'returns the explicitely set home dir'
do
GPGME
::
Engine
.
home_dir
=
'/tmp/gpg'
expect
(
described_class
.
current_home_dir
).
to
eq
'/tmp/gpg'
GPGME
::
Engine
.
home_dir
=
GPGME
::
Engine
.
dirinfo
(
'homedir'
)
end
it
'returns the default value when explicitely setting the home dir to nil'
do
GPGME
::
Engine
.
home_dir
=
nil
expect
(
described_class
.
current_home_dir
).
to
eq
default_home_dir
end
end
describe
'.using_tmp_keychain'
do
it
"the second thread does not change the first thread's directory"
do
thread1
=
Thread
.
new
do
described_class
.
using_tmp_keychain
do
dir
=
described_class
.
current_home_dir
sleep
0.1
expect
(
described_class
.
current_home_dir
).
to
eq
dir
end
end
thread2
=
Thread
.
new
do
described_class
.
using_tmp_keychain
do
sleep
0.2
end
end
thread1
.
join
thread2
.
join
end
it
'allows recursive execution in the same thread'
do
expect
do
described_class
.
using_tmp_keychain
do
described_class
.
using_tmp_keychain
do
end
end
end
.
not_to
raise_error
(
ThreadError
)
end
end
end
describe
Gitlab
::
Gpg
::
CurrentKeyChain
do
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment