@@ -21,6 +21,7 @@ import (
2121 "github.com/Microsoft/hcsshim/internal/oc"
2222 "github.com/Microsoft/hcsshim/internal/timeout"
2323 "github.com/Microsoft/hcsshim/internal/vmcompute"
24+ "github.com/Microsoft/hcsshim/internal/winapi"
2425 "github.com/sirupsen/logrus"
2526 "go.opencensus.io/trace"
2627)
@@ -206,7 +207,7 @@ func (computeSystem *System) Start(ctx context.Context) (err error) {
206207
207208 // prevent starting an exited system because waitblock we do not recreate waitBlock
208209 // or rerun waitBackground, so we have no way to be notified of it closing again
209- if computeSystem .handle == 0 {
210+ if winapi . IsInvalidHandle ( computeSystem .handle ) {
210211 return makeSystemError (computeSystem , operation , ErrAlreadyClosed , nil )
211212 }
212213
@@ -232,7 +233,7 @@ func (computeSystem *System) Shutdown(ctx context.Context) error {
232233
233234 operation := "hcs::System::Shutdown"
234235
235- if computeSystem .handle == 0 || computeSystem .stopped () {
236+ if winapi . IsInvalidHandle ( computeSystem .handle ) || computeSystem .stopped () {
236237 return nil
237238 }
238239
@@ -254,7 +255,7 @@ func (computeSystem *System) Terminate(ctx context.Context) error {
254255
255256 operation := "hcs::System::Terminate"
256257
257- if computeSystem .handle == 0 || computeSystem .stopped () {
258+ if winapi . IsInvalidHandle ( computeSystem .handle ) || computeSystem .stopped () {
258259 return nil
259260 }
260261
@@ -351,7 +352,7 @@ func (computeSystem *System) Properties(ctx context.Context, types ...schema1.Pr
351352
352353 operation := "hcs::System::Properties"
353354
354- if computeSystem .handle == 0 {
355+ if winapi . IsInvalidHandle ( computeSystem .handle ) {
355356 return nil , makeSystemError (computeSystem , operation , ErrAlreadyClosed , nil )
356357 }
357358
@@ -492,7 +493,7 @@ func (computeSystem *System) statisticsInProc(job *jobobject.JobObject) (*hcssch
492493func (computeSystem * System ) hcsPropertiesV2Query (ctx context.Context , types []hcsschema.PropertyType ) (* hcsschema.Properties , error ) {
493494 operation := "hcs::System::PropertiesV2"
494495
495- if computeSystem .handle == 0 {
496+ if winapi . IsInvalidHandle ( computeSystem .handle ) {
496497 return nil , makeSystemError (computeSystem , operation , ErrAlreadyClosed , nil )
497498 }
498499
@@ -586,7 +587,7 @@ func (computeSystem *System) Pause(ctx context.Context) (err error) {
586587 computeSystem .handleLock .RLock ()
587588 defer computeSystem .handleLock .RUnlock ()
588589
589- if computeSystem .handle == 0 {
590+ if winapi . IsInvalidHandle ( computeSystem .handle ) {
590591 return makeSystemError (computeSystem , operation , ErrAlreadyClosed , nil )
591592 }
592593
@@ -614,7 +615,7 @@ func (computeSystem *System) Resume(ctx context.Context) (err error) {
614615 computeSystem .handleLock .RLock ()
615616 defer computeSystem .handleLock .RUnlock ()
616617
617- if computeSystem .handle == 0 {
618+ if winapi . IsInvalidHandle ( computeSystem .handle ) {
618619 return makeSystemError (computeSystem , operation , ErrAlreadyClosed , nil )
619620 }
620621
@@ -647,7 +648,7 @@ func (computeSystem *System) Save(ctx context.Context, options interface{}) (err
647648 computeSystem .handleLock .RLock ()
648649 defer computeSystem .handleLock .RUnlock ()
649650
650- if computeSystem .handle == 0 {
651+ if winapi . IsInvalidHandle ( computeSystem .handle ) {
651652 return makeSystemError (computeSystem , operation , ErrAlreadyClosed , nil )
652653 }
653654
@@ -665,7 +666,7 @@ func (computeSystem *System) createProcess(ctx context.Context, operation string
665666 computeSystem .handleLock .RLock ()
666667 defer computeSystem .handleLock .RUnlock ()
667668
668- if computeSystem .handle == 0 {
669+ if winapi . IsInvalidHandle ( computeSystem .handle ) {
669670 return nil , nil , makeSystemError (computeSystem , operation , ErrAlreadyClosed , nil )
670671 }
671672
@@ -727,7 +728,7 @@ func (computeSystem *System) OpenProcess(ctx context.Context, pid int) (*Process
727728
728729 operation := "hcs::System::OpenProcess"
729730
730- if computeSystem .handle == 0 {
731+ if winapi . IsInvalidHandle ( computeSystem .handle ) {
731732 return nil , makeSystemError (computeSystem , operation , ErrAlreadyClosed , nil )
732733 }
733734
@@ -766,7 +767,7 @@ func (computeSystem *System) CloseCtx(ctx context.Context) (err error) {
766767 defer computeSystem .handleLock .Unlock ()
767768
768769 // Don't double free this
769- if computeSystem .handle == 0 {
770+ if winapi . IsInvalidHandle ( computeSystem .handle ) {
770771 return nil
771772 }
772773
@@ -789,14 +790,19 @@ func (computeSystem *System) CloseCtx(ctx context.Context) (err error) {
789790}
790791
791792func (computeSystem * System ) registerCallback (ctx context.Context ) error {
793+ callbackNumber := nextCallback .Inc ()
794+
795+ log .G (ctx ).WithFields (logrus.Fields {
796+ "cid" : computeSystem .id ,
797+ "callbackNumber" : callbackNumber ,
798+ }).Debug ("register computer system callback" )
799+
792800 callbackContext := & notificationWatcherContext {
793801 channels : newSystemChannels (),
794802 systemID : computeSystem .id ,
795803 }
796804
797805 callbackMapLock .Lock ()
798- callbackNumber := nextCallback
799- nextCallback ++
800806 callbackMap [callbackNumber ] = callbackContext
801807 callbackMapLock .Unlock ()
802808
@@ -814,6 +820,11 @@ func (computeSystem *System) registerCallback(ctx context.Context) error {
814820func (computeSystem * System ) unregisterCallback (ctx context.Context ) error {
815821 callbackNumber := computeSystem .callbackNumber
816822
823+ log .G (ctx ).WithFields (logrus.Fields {
824+ "cid" : computeSystem .id ,
825+ "callbackNumber" : callbackNumber ,
826+ }).Debug ("unregister computer system callback" )
827+
817828 callbackMapLock .RLock ()
818829 callbackContext := callbackMap [callbackNumber ]
819830 callbackMapLock .RUnlock ()
@@ -824,7 +835,7 @@ func (computeSystem *System) unregisterCallback(ctx context.Context) error {
824835
825836 handle := callbackContext .handle
826837
827- if handle == 0 {
838+ if winapi . IsInvalidHandle ( handle ) {
828839 return nil
829840 }
830841
@@ -853,7 +864,7 @@ func (computeSystem *System) Modify(ctx context.Context, config interface{}) err
853864
854865 operation := "hcs::System::Modify"
855866
856- if computeSystem .handle == 0 {
867+ if winapi . IsInvalidHandle ( computeSystem .handle ) {
857868 return makeSystemError (computeSystem , operation , ErrAlreadyClosed , nil )
858869 }
859870
0 commit comments