@@ -24,6 +24,7 @@ type BrowserProcess struct {
24
24
// Channels for managing termination.
25
25
lostConnection chan struct {}
26
26
processIsGracefullyClosing chan struct {}
27
+ processDone chan struct {}
27
28
28
29
// Browser's WebSocket URL to speak CDP
29
30
wsURL string
@@ -38,26 +39,23 @@ func NewBrowserProcess(
38
39
ctx context.Context , path string , args , env []string , dataDir * storage.Dir ,
39
40
ctxCancel context.CancelFunc , logger * log.Logger ,
40
41
) (* BrowserProcess , error ) {
41
- procCtx , procCtxCancel := context .WithCancel (ctx )
42
- cmd , err := execute (
43
- procCtx , procCtxCancel , path , args , env , dataDir , logger )
42
+ cmd , err := execute (ctx , ctxCancel , path , args , env , dataDir , logger )
44
43
if err != nil {
45
- procCtxCancel ()
46
44
return nil , err
47
45
}
48
46
49
- wsURL , err := parseDevToolsURL (cmd )
47
+ wsURL , err := parseDevToolsURL (ctx , cmd )
50
48
if err != nil {
51
- procCtxCancel ()
52
49
return nil , err
53
50
}
54
51
55
52
p := BrowserProcess {
56
- ctx : procCtx ,
53
+ ctx : ctx ,
57
54
cancel : ctxCancel ,
58
55
process : cmd .Process ,
59
56
lostConnection : make (chan struct {}),
60
57
processIsGracefullyClosing : make (chan struct {}),
58
+ processDone : cmd .done ,
61
59
wsURL : wsURL ,
62
60
userDataDir : dataDir ,
63
61
}
@@ -67,7 +65,7 @@ func NewBrowserProcess(
67
65
// browser-initiated termination then cancel the context to clean up.
68
66
select {
69
67
case <- p .lostConnection :
70
- case <- procCtx .Done ():
68
+ case <- ctx .Done ():
71
69
}
72
70
73
71
select {
@@ -123,7 +121,7 @@ func (p *BrowserProcess) AttachLogger(logger *log.Logger) {
123
121
124
122
type command struct {
125
123
* exec.Cmd
126
- ctx context. Context
124
+ done chan struct {}
127
125
stdout , stderr io.Reader
128
126
}
129
127
@@ -161,13 +159,14 @@ func execute(
161
159
return command {}, fmt .Errorf ("%w" , ctx .Err ())
162
160
}
163
161
162
+ done := make (chan struct {})
164
163
go func () {
165
164
// TODO: How to handle these errors?
166
165
defer func () {
167
166
if err := dataDir .Cleanup (); err != nil {
168
167
logger .Errorf ("browser" , "cleaning up the user data directory: %v" , err )
169
168
}
170
- ctxCancel ( )
169
+ close ( done )
171
170
}()
172
171
173
172
if err := cmd .Wait (); err != nil {
@@ -177,11 +176,11 @@ func execute(
177
176
}
178
177
}()
179
178
180
- return command {cmd , ctx , stdout , stderr }, nil
179
+ return command {cmd , done , stdout , stderr }, nil
181
180
}
182
181
183
182
// parseDevToolsURL grabs the websocket address from chrome's output and returns it.
184
- func parseDevToolsURL (cmd command ) (wsURL string , _ error ) {
183
+ func parseDevToolsURL (ctx context. Context , cmd command ) (wsURL string , _ error ) {
185
184
type result struct {
186
185
devToolsURL string
187
186
err error
@@ -214,7 +213,7 @@ func parseDevToolsURL(cmd command) (wsURL string, _ error) {
214
213
select {
215
214
case r := <- c :
216
215
return r .devToolsURL , r .err
217
- case <- cmd . ctx .Done ():
218
- return "" , fmt .Errorf ("%w" , cmd . ctx .Err ())
216
+ case <- ctx .Done ():
217
+ return "" , fmt .Errorf ("%w" , ctx .Err ())
219
218
}
220
219
}
0 commit comments