2016-05-23 19:12:06 -04:00
|
|
|
package hcsshim
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2016-09-22 16:52:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
)
|
2016-05-23 19:12:06 -04:00
|
|
|
|
2016-05-31 15:51:29 -04:00
|
|
|
func processAsyncHcsResult(err error, resultp *uint16, callbackNumber uintptr, expectedNotification hcsNotification, timeout *time.Duration) error {
|
2016-05-23 19:12:06 -04:00
|
|
|
err = processHcsResult(err, resultp)
|
2016-08-03 20:47:43 -04:00
|
|
|
if IsPending(err) {
|
2016-05-31 15:51:29 -04:00
|
|
|
return waitForNotification(callbackNumber, expectedNotification, timeout)
|
2016-05-23 19:12:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-05-31 15:51:29 -04:00
|
|
|
func waitForNotification(callbackNumber uintptr, expectedNotification hcsNotification, timeout *time.Duration) error {
|
2016-05-23 19:12:06 -04:00
|
|
|
callbackMapLock.RLock()
|
2016-05-31 15:51:29 -04:00
|
|
|
channels := callbackMap[callbackNumber].channels
|
2016-05-23 19:12:06 -04:00
|
|
|
callbackMapLock.RUnlock()
|
|
|
|
|
2016-05-31 15:51:29 -04:00
|
|
|
expectedChannel := channels[expectedNotification]
|
|
|
|
if expectedChannel == nil {
|
|
|
|
logrus.Errorf("unknown notification type in waitForNotification %x", expectedNotification)
|
2016-06-10 18:27:05 -04:00
|
|
|
return ErrInvalidNotificationType
|
2016-05-23 19:12:06 -04:00
|
|
|
}
|
|
|
|
|
2016-11-08 14:36:07 -05:00
|
|
|
var c <-chan time.Time
|
2016-05-31 15:51:29 -04:00
|
|
|
if timeout != nil {
|
|
|
|
timer := time.NewTimer(*timeout)
|
2016-11-08 14:36:07 -05:00
|
|
|
c = timer.C
|
2016-05-31 15:51:29 -04:00
|
|
|
defer timer.Stop()
|
|
|
|
}
|
2016-11-08 14:36:07 -05:00
|
|
|
|
2016-05-23 19:12:06 -04:00
|
|
|
select {
|
2016-06-10 18:27:05 -04:00
|
|
|
case err, ok := <-expectedChannel:
|
|
|
|
if !ok {
|
|
|
|
return ErrHandleClose
|
|
|
|
}
|
2016-05-23 19:12:06 -04:00
|
|
|
return err
|
2016-06-10 18:27:05 -04:00
|
|
|
case err, ok := <-channels[hcsNotificationSystemExited]:
|
|
|
|
if !ok {
|
|
|
|
return ErrHandleClose
|
|
|
|
}
|
2016-05-31 15:51:29 -04:00
|
|
|
// If the expected notification is hcsNotificationSystemExited which of the two selects
|
|
|
|
// chosen is random. Return the raw error if hcsNotificationSystemExited is expected
|
|
|
|
if channels[hcsNotificationSystemExited] == expectedChannel {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ErrUnexpectedContainerExit
|
2016-06-10 18:27:05 -04:00
|
|
|
case _, ok := <-channels[hcsNotificationServiceDisconnect]:
|
|
|
|
if !ok {
|
|
|
|
return ErrHandleClose
|
|
|
|
}
|
2016-05-31 15:51:29 -04:00
|
|
|
// hcsNotificationServiceDisconnect should never be an expected notification
|
|
|
|
// it does not need the same handling as hcsNotificationSystemExited
|
|
|
|
return ErrUnexpectedProcessAbort
|
2016-11-08 14:36:07 -05:00
|
|
|
case <-c:
|
|
|
|
return ErrTimeout
|
2016-05-23 19:12:06 -04:00
|
|
|
}
|
|
|
|
}
|