Browse code

Vendor Microsoft/hcsshim@ba3d6667

Signed-off-by: John Howard <jhoward@microsoft.com>

John Howard authored on 2019/03/28 06:12:40
Showing 4 changed files
... ...
@@ -1,6 +1,6 @@
1 1
 # the following lines are in sorted order, FYI
2 2
 github.com/Azure/go-ansiterm d6e3b3328b783f23731bc4d058875b0371ff8109
3
-github.com/Microsoft/hcsshim ada9cb39f715fb568e1030e7613732bb4f1e4aeb
3
+github.com/Microsoft/hcsshim ba3d6667710fa905116f39a19d059c4c1016be7c
4 4
 github.com/Microsoft/go-winio c599b533b43b1363d7d7c6cfda5ede70ed73ff13
5 5
 github.com/docker/libtrust 9cbd2a1374f46905c68a4eb3694a130610adc62a
6 6
 github.com/go-check/check 4ed411733c5785b40214c70bce814c3a3a689609 https://github.com/cpuguy83/check.git
... ...
@@ -23,6 +23,10 @@ type Process struct {
23 23
 	callbackNumber uintptr
24 24
 
25 25
 	logctx logrus.Fields
26
+
27
+	closedWaitOnce sync.Once
28
+	waitBlock      chan struct{}
29
+	waitError      error
26 30
 }
27 31
 
28 32
 func newProcess(process hcsProcess, processID int, computeSystem *System) *Process {
... ...
@@ -34,6 +38,7 @@ func newProcess(process hcsProcess, processID int, computeSystem *System) *Proce
34 34
 			logfields.ContainerID: computeSystem.ID(),
35 35
 			logfields.ProcessID:   processID,
36 36
 		},
37
+		waitBlock: make(chan struct{}),
37 38
 	}
38 39
 }
39 40
 
... ...
@@ -163,33 +168,49 @@ func (process *Process) Kill() (err error) {
163 163
 	return nil
164 164
 }
165 165
 
166
-// Wait waits for the process to exit.
166
+// waitBackground waits for the process exit notification. Once received sets
167
+// `process.waitError` (if any) and unblocks all `Wait` and `WaitTimeout` calls.
168
+//
169
+// This MUST be called exactly once per `process.handle` but `Wait` and
170
+// `WaitTimeout` are safe to call multiple times.
171
+func (process *Process) waitBackground() {
172
+	process.waitError = waitForNotification(process.callbackNumber, hcsNotificationProcessExited, nil)
173
+	process.closedWaitOnce.Do(func() {
174
+		close(process.waitBlock)
175
+	})
176
+}
177
+
178
+// Wait waits for the process to exit. If the process has already exited returns
179
+// the pervious error (if any).
167 180
 func (process *Process) Wait() (err error) {
168 181
 	operation := "hcsshim::Process::Wait"
169 182
 	process.logOperationBegin(operation)
170 183
 	defer func() { process.logOperationEnd(operation, err) }()
171 184
 
172
-	err = waitForNotification(process.callbackNumber, hcsNotificationProcessExited, nil)
173
-	if err != nil {
185
+	<-process.waitBlock
186
+	if process.waitError != nil {
174 187
 		return makeProcessError(process, operation, err, nil)
175 188
 	}
176
-
177 189
 	return nil
178 190
 }
179 191
 
180
-// WaitTimeout waits for the process to exit or the duration to elapse. It returns
181
-// false if timeout occurs.
192
+// WaitTimeout waits for the process to exit or the duration to elapse. If the
193
+// process has already exited returns the pervious error (if any). If a timeout
194
+// occurs returns `ErrTimeout`.
182 195
 func (process *Process) WaitTimeout(timeout time.Duration) (err error) {
183 196
 	operation := "hcssshim::Process::WaitTimeout"
184 197
 	process.logOperationBegin(operation)
185 198
 	defer func() { process.logOperationEnd(operation, err) }()
186 199
 
187
-	err = waitForNotification(process.callbackNumber, hcsNotificationProcessExited, &timeout)
188
-	if err != nil {
189
-		return makeProcessError(process, operation, err, nil)
200
+	select {
201
+	case <-process.waitBlock:
202
+		if process.waitError != nil {
203
+			return makeProcessError(process, operation, process.waitError, nil)
204
+		}
205
+		return nil
206
+	case <-time.After(timeout):
207
+		return makeProcessError(process, operation, ErrTimeout, nil)
190 208
 	}
191
-
192
-	return nil
193 209
 }
194 210
 
195 211
 // ResizeConsole resizes the console of the process.
... ...
@@ -402,6 +423,9 @@ func (process *Process) Close() (err error) {
402 402
 	}
403 403
 
404 404
 	process.handle = 0
405
+	process.closedWaitOnce.Do(func() {
406
+		close(process.waitBlock)
407
+	})
405 408
 
406 409
 	return nil
407 410
 }
... ...
@@ -43,6 +43,10 @@ type System struct {
43 43
 	callbackNumber uintptr
44 44
 
45 45
 	logctx logrus.Fields
46
+
47
+	closedWaitOnce sync.Once
48
+	waitBlock      chan struct{}
49
+	waitError      error
46 50
 }
47 51
 
48 52
 func newSystem(id string) *System {
... ...
@@ -51,6 +55,7 @@ func newSystem(id string) *System {
51 51
 		logctx: logrus.Fields{
52 52
 			logfields.ContainerID: id,
53 53
 		},
54
+		waitBlock: make(chan struct{}),
54 55
 	}
55 56
 }
56 57
 
... ...
@@ -121,6 +126,8 @@ func CreateComputeSystem(id string, hcsDocumentInterface interface{}) (_ *System
121 121
 		return nil, makeSystemError(computeSystem, operation, hcsDocument, err, events)
122 122
 	}
123 123
 
124
+	go computeSystem.waitBackground()
125
+
124 126
 	return computeSystem, nil
125 127
 }
126 128
 
... ...
@@ -153,6 +160,7 @@ func OpenComputeSystem(id string) (_ *System, err error) {
153 153
 	if err = computeSystem.registerCallback(); err != nil {
154 154
 		return nil, makeSystemError(computeSystem, operation, "", err, nil)
155 155
 	}
156
+	go computeSystem.waitBackground()
156 157
 
157 158
 	return computeSystem, nil
158 159
 }
... ...
@@ -280,7 +288,7 @@ func (computeSystem *System) Shutdown() (err error) {
280 280
 	operation := "hcsshim::ComputeSystem::Shutdown"
281 281
 	computeSystem.logOperationBegin(operation)
282 282
 	defer func() {
283
-		if IsAlreadyStopped(err) || IsPending(err) {
283
+		if IsAlreadyClosed(err) || IsAlreadyStopped(err) || IsPending(err) {
284 284
 			computeSystem.logOperationEnd(operation, nil)
285 285
 		} else {
286 286
 			computeSystem.logOperationEnd(operation, err)
... ...
@@ -312,7 +320,7 @@ func (computeSystem *System) Terminate() (err error) {
312 312
 	operation := "hcsshim::ComputeSystem::Terminate"
313 313
 	computeSystem.logOperationBegin(operation)
314 314
 	defer func() {
315
-		if IsPending(err) {
315
+		if IsAlreadyClosed(err) || IsAlreadyStopped(err) || IsPending(err) {
316 316
 			computeSystem.logOperationEnd(operation, nil)
317 317
 		} else {
318 318
 			computeSystem.logOperationEnd(operation, err)
... ...
@@ -335,48 +343,67 @@ func (computeSystem *System) Terminate() (err error) {
335 335
 	return nil
336 336
 }
337 337
 
338
-// Wait synchronously waits for the compute system to shutdown or terminate.
338
+// waitBackground waits for the compute system exit notification. Once received
339
+// sets `computeSystem.waitError` (if any) and unblocks all `Wait`,
340
+// `WaitExpectedError`, and `WaitTimeout` calls.
341
+//
342
+// This MUST be called exactly once per `computeSystem.handle` but `Wait`,
343
+// `WaitExpectedError`, and `WaitTimeout` are safe to call multiple times.
344
+func (computeSystem *System) waitBackground() {
345
+	computeSystem.waitError = waitForNotification(computeSystem.callbackNumber, hcsNotificationSystemExited, nil)
346
+	computeSystem.closedWaitOnce.Do(func() {
347
+		close(computeSystem.waitBlock)
348
+	})
349
+}
350
+
351
+// Wait synchronously waits for the compute system to shutdown or terminate. If
352
+// the compute system has already exited returns the previous error (if any).
339 353
 func (computeSystem *System) Wait() (err error) {
340 354
 	operation := "hcsshim::ComputeSystem::Wait"
341 355
 	computeSystem.logOperationBegin(operation)
342 356
 	defer func() { computeSystem.logOperationEnd(operation, err) }()
343 357
 
344
-	err = waitForNotification(computeSystem.callbackNumber, hcsNotificationSystemExited, nil)
345
-	if err != nil {
346
-		return makeSystemError(computeSystem, "Wait", "", err, nil)
358
+	<-computeSystem.waitBlock
359
+	if computeSystem.waitError != nil {
360
+		return makeSystemError(computeSystem, "Wait", "", computeSystem.waitError, nil)
347 361
 	}
348 362
 
349 363
 	return nil
350 364
 }
351 365
 
352 366
 // WaitExpectedError synchronously waits for the compute system to shutdown or
353
-// terminate, and ignores the passed error if it occurs.
367
+// terminate and returns the error (if any) as long as it does not match
368
+// `expected`. If the compute system has already exited returns the previous
369
+// error (if any) as long as it does not match `expected`.
354 370
 func (computeSystem *System) WaitExpectedError(expected error) (err error) {
355 371
 	operation := "hcsshim::ComputeSystem::WaitExpectedError"
356 372
 	computeSystem.logOperationBegin(operation)
357 373
 	defer func() { computeSystem.logOperationEnd(operation, err) }()
358 374
 
359
-	err = waitForNotification(computeSystem.callbackNumber, hcsNotificationSystemExited, nil)
360
-	if err != nil && getInnerError(err) != expected {
361
-		return makeSystemError(computeSystem, "WaitExpectedError", "", err, nil)
375
+	<-computeSystem.waitBlock
376
+	if computeSystem.waitError != nil && getInnerError(computeSystem.waitError) != expected {
377
+		return makeSystemError(computeSystem, "WaitExpectedError", "", computeSystem.waitError, nil)
362 378
 	}
363
-
364 379
 	return nil
365 380
 }
366 381
 
367
-// WaitTimeout synchronously waits for the compute system to terminate or the duration to elapse.
368
-// If the timeout expires, IsTimeout(err) == true
382
+// WaitTimeout synchronously waits for the compute system to terminate or the
383
+// duration to elapse. If the timeout expires, `IsTimeout(err) == true`. If
384
+// the compute system has already exited returns the previous error (if any).
369 385
 func (computeSystem *System) WaitTimeout(timeout time.Duration) (err error) {
370 386
 	operation := "hcsshim::ComputeSystem::WaitTimeout"
371 387
 	computeSystem.logOperationBegin(operation)
372 388
 	defer func() { computeSystem.logOperationEnd(operation, err) }()
373 389
 
374
-	err = waitForNotification(computeSystem.callbackNumber, hcsNotificationSystemExited, &timeout)
375
-	if err != nil {
376
-		return makeSystemError(computeSystem, "WaitTimeout", "", err, nil)
390
+	select {
391
+	case <-computeSystem.waitBlock:
392
+		if computeSystem.waitError != nil {
393
+			return makeSystemError(computeSystem, "WaitTimeout", "", computeSystem.waitError, nil)
394
+		}
395
+		return nil
396
+	case <-time.After(timeout):
397
+		return makeSystemError(computeSystem, "WaitTimeout", "", ErrTimeout, nil)
377 398
 	}
378
-
379
-	return nil
380 399
 }
381 400
 
382 401
 func (computeSystem *System) Properties(types ...schema1.PropertyType) (_ *schema1.ContainerProperties, err error) {
... ...
@@ -519,6 +546,7 @@ func (computeSystem *System) CreateProcess(c interface{}) (_ *Process, err error
519 519
 	if err = process.registerCallback(); err != nil {
520 520
 		return nil, makeSystemError(computeSystem, "CreateProcess", "", err, nil)
521 521
 	}
522
+	go process.waitBackground()
522 523
 
523 524
 	return process, nil
524 525
 }
... ...
@@ -557,6 +585,7 @@ func (computeSystem *System) OpenProcess(pid int) (_ *Process, err error) {
557 557
 	if err = process.registerCallback(); err != nil {
558 558
 		return nil, makeSystemError(computeSystem, "OpenProcess", "", err, nil)
559 559
 	}
560
+	go process.waitBackground()
560 561
 
561 562
 	return process, nil
562 563
 }
... ...
@@ -587,6 +616,9 @@ func (computeSystem *System) Close() (err error) {
587 587
 	}
588 588
 
589 589
 	computeSystem.handle = 0
590
+	computeSystem.closedWaitOnce.Do(func() {
591
+		close(computeSystem.waitBlock)
592
+	})
590 593
 
591 594
 	return nil
592 595
 }
... ...
@@ -10,7 +10,7 @@ github.com/hashicorp/errwrap 7554cd9344cec97297fa6649b055a8c98c2a1e55
10 10
 github.com/hashicorp/go-multierror ed905158d87462226a13fe39ddf685ea65f1c11f
11 11
 github.com/konsorten/go-windows-terminal-sequences v1.0.1
12 12
 github.com/linuxkit/virtsock 8e79449dea0735c1c056d814934dd035734cc97c
13
-github.com/Microsoft/go-winio 16cfc975803886a5e47c4257a24c8d8c52e178b2
13
+github.com/Microsoft/go-winio c599b533b43b1363d7d7c6cfda5ede70ed73ff13
14 14
 github.com/Microsoft/opengcs v0.3.9
15 15
 github.com/opencontainers/go-digest c9281466c8b2f606084ac71339773efd177436e7
16 16
 github.com/opencontainers/runc 12f6a991201fdb8f82579582d5e00e28fba06d0a