Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
packer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kristopher Ruzic
packer
Commits
26a117e3
Commit
26a117e3
authored
Jul 29, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
packer: use locks/conds to avoid races on RemoteCmd.Exited [GH-42]
parent
0363a1cd
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
5 deletions
+37
-5
packer/communicator.go
packer/communicator.go
+35
-2
packer/communicator_test.go
packer/communicator_test.go
+2
-3
No files found.
packer/communicator.go
View file @
26a117e3
...
@@ -4,7 +4,7 @@ import (
...
@@ -4,7 +4,7 @@ import (
"github.com/mitchellh/iochan"
"github.com/mitchellh/iochan"
"io"
"io"
"strings"
"strings"
"
time
"
"
sync
"
)
)
// RemoteCmd represents a remote command being prepared or run.
// RemoteCmd represents a remote command being prepared or run.
...
@@ -32,6 +32,10 @@ type RemoteCmd struct {
...
@@ -32,6 +32,10 @@ type RemoteCmd struct {
// Once Exited is true, this will contain the exit code of the process.
// Once Exited is true, this will contain the exit code of the process.
ExitStatus
int
ExitStatus
int
// Internal locks and such used for safely setting some shared variables
l
sync
.
Mutex
exitCond
*
sync
.
Cond
}
}
// A Communicator is the interface used to communicate with the machine
// A Communicator is the interface used to communicate with the machine
...
@@ -125,9 +129,38 @@ OutputLoop:
...
@@ -125,9 +129,38 @@ OutputLoop:
return
nil
return
nil
}
}
// SetExited is a helper for setting that this process is exited. This
// should be called by communicators who are running a remote command in
// order to set that the command is done.
func
(
r
*
RemoteCmd
)
SetExited
(
status
int
)
{
r
.
l
.
Lock
()
if
r
.
exitCond
==
nil
{
r
.
exitCond
=
sync
.
NewCond
(
new
(
sync
.
Mutex
))
}
r
.
l
.
Unlock
()
r
.
exitCond
.
L
.
Lock
()
defer
r
.
exitCond
.
L
.
Unlock
()
r
.
Exited
=
true
r
.
ExitStatus
=
status
r
.
exitCond
.
Broadcast
()
}
// Wait waits for the remote command to complete.
// Wait waits for the remote command to complete.
func
(
r
*
RemoteCmd
)
Wait
()
{
func
(
r
*
RemoteCmd
)
Wait
()
{
// Make sure our condition variable is initialized.
r
.
l
.
Lock
()
if
r
.
exitCond
==
nil
{
r
.
exitCond
=
sync
.
NewCond
(
new
(
sync
.
Mutex
))
}
r
.
l
.
Unlock
()
// Wait on the condition variable to notify we exited
r
.
exitCond
.
L
.
Lock
()
defer
r
.
exitCond
.
L
.
Unlock
()
for
!
r
.
Exited
{
for
!
r
.
Exited
{
time
.
Sleep
(
50
*
time
.
Millisecond
)
r
.
exitCond
.
Wait
(
)
}
}
}
}
packer/communicator_test.go
View file @
26a117e3
...
@@ -59,7 +59,7 @@ func TestRemoteCmd_StartWithUi(t *testing.T) {
...
@@ -59,7 +59,7 @@ func TestRemoteCmd_StartWithUi(t *testing.T) {
go
func
()
{
go
func
()
{
time
.
Sleep
(
100
*
time
.
Millisecond
)
time
.
Sleep
(
100
*
time
.
Millisecond
)
rc
.
Exited
=
true
rc
.
SetExited
(
0
)
}()
}()
err
:=
rc
.
StartWithUi
(
testComm
,
testUi
)
err
:=
rc
.
StartWithUi
(
testComm
,
testUi
)
...
@@ -85,8 +85,7 @@ func TestRemoteCmd_Wait(t *testing.T) {
...
@@ -85,8 +85,7 @@ func TestRemoteCmd_Wait(t *testing.T) {
result
<-
true
result
<-
true
}()
}()
cmd
.
ExitStatus
=
42
cmd
.
SetExited
(
42
)
cmd
.
Exited
=
true
select
{
select
{
case
<-
result
:
case
<-
result
:
...
...
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