2018-09-05 14:54:38 -04:00
|
|
|
package hcs
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
import (
|
2020-01-06 12:15:00 -05:00
|
|
|
"context"
|
2018-03-19 18:57:30 -04:00
|
|
|
"time"
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
"github.com/Microsoft/hcsshim/internal/log"
|
2018-03-19 18:57:30 -04:00
|
|
|
)
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
func processAsyncHcsResult(ctx context.Context, err error, resultJSON string, callbackNumber uintptr, expectedNotification hcsNotification, timeout *time.Duration) ([]ErrorEvent, error) {
|
|
|
|
events := processHcsResult(ctx, resultJSON)
|
2018-03-19 18:57:30 -04:00
|
|
|
if IsPending(err) {
|
2020-01-06 12:15:00 -05:00
|
|
|
return nil, waitForNotification(ctx, callbackNumber, expectedNotification, timeout)
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
return events, err
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
func waitForNotification(ctx context.Context, callbackNumber uintptr, expectedNotification hcsNotification, timeout *time.Duration) error {
|
2018-03-19 18:57:30 -04:00
|
|
|
callbackMapLock.RLock()
|
2019-04-03 15:06:12 -04:00
|
|
|
if _, ok := callbackMap[callbackNumber]; !ok {
|
|
|
|
callbackMapLock.RUnlock()
|
2020-01-06 12:15:00 -05:00
|
|
|
log.G(ctx).WithField("callbackNumber", callbackNumber).Error("failed to waitForNotification: callbackNumber does not exist in callbackMap")
|
2019-04-03 15:06:12 -04:00
|
|
|
return ErrHandleClose
|
|
|
|
}
|
2018-03-19 18:57:30 -04:00
|
|
|
channels := callbackMap[callbackNumber].channels
|
|
|
|
callbackMapLock.RUnlock()
|
|
|
|
|
|
|
|
expectedChannel := channels[expectedNotification]
|
|
|
|
if expectedChannel == nil {
|
2020-01-06 12:15:00 -05:00
|
|
|
log.G(ctx).WithField("type", expectedNotification).Error("unknown notification type in waitForNotification")
|
2018-03-19 18:57:30 -04:00
|
|
|
return ErrInvalidNotificationType
|
|
|
|
}
|
|
|
|
|
|
|
|
var c <-chan time.Time
|
|
|
|
if timeout != nil {
|
|
|
|
timer := time.NewTimer(*timeout)
|
|
|
|
c = timer.C
|
|
|
|
defer timer.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err, ok := <-expectedChannel:
|
|
|
|
if !ok {
|
|
|
|
return ErrHandleClose
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
case err, ok := <-channels[hcsNotificationSystemExited]:
|
|
|
|
if !ok {
|
|
|
|
return ErrHandleClose
|
|
|
|
}
|
|
|
|
// 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
|
|
|
|
case _, ok := <-channels[hcsNotificationServiceDisconnect]:
|
|
|
|
if !ok {
|
|
|
|
return ErrHandleClose
|
|
|
|
}
|
|
|
|
// hcsNotificationServiceDisconnect should never be an expected notification
|
|
|
|
// it does not need the same handling as hcsNotificationSystemExited
|
|
|
|
return ErrUnexpectedProcessAbort
|
|
|
|
case <-c:
|
|
|
|
return ErrTimeout
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|