@@ -13,6 +13,7 @@ import (
1313 "github.com/drone-runners/drone-runner-docker/internal/docker/image"
1414 "github.com/drone-runners/drone-runner-docker/internal/docker/jsonmessage"
1515 "github.com/drone-runners/drone-runner-docker/internal/docker/stdcopy"
16+ "github.com/drone/runner-go/logger"
1617 "github.com/drone/runner-go/pipeline/runtime"
1718 "github.com/drone/runner-go/registry/auths"
1819
@@ -156,7 +157,7 @@ func (e *Docker) Run(ctx context.Context, specv runtime.Spec, stepv runtime.Step
156157 return nil , errors .TrimExtraInfo (err )
157158 }
158159 // wait for the response
159- return e .wait (ctx , step .ID )
160+ return e .waitRetry (ctx , step .ID )
160161}
161162
162163//
@@ -247,6 +248,29 @@ func (e *Docker) start(ctx context.Context, id string) error {
247248 return e .client .ContainerStart (ctx , id , types.ContainerStartOptions {})
248249}
249250
251+ // helper function emulates the `docker wait` command, blocking
252+ // until the container stops and returning the exit code.
253+ func (e * Docker ) waitRetry (ctx context.Context , id string ) (* runtime.State , error ) {
254+ for {
255+ // if the context is canceled, meaning the
256+ // pipeline timed out or was killed by the
257+ // end-user, we should exit with an error.
258+ if err := ctx .Err (); err != nil {
259+ return nil , err
260+ }
261+ state , err := e .wait (ctx , id )
262+ if err != nil {
263+ return nil , err
264+ }
265+ if state .Exited {
266+ return state , err
267+ }
268+ logger .FromContext (ctx ).
269+ WithField ("container" , id ).
270+ Trace ("docker wait exited unexpectedly" )
271+ }
272+ }
273+
250274// helper function emulates the `docker wait` command, blocking
251275// until the container stops and returning the exit code.
252276func (e * Docker ) wait (ctx context.Context , id string ) (* runtime.State , error ) {
@@ -260,13 +284,9 @@ func (e *Docker) wait(ctx context.Context, id string) (*runtime.State, error) {
260284 if err != nil {
261285 return nil , err
262286 }
263- if info .State .Running {
264- // TODO(bradrydewski) if the state is still running
265- // we should call wait again.
266- }
267287
268288 return & runtime.State {
269- Exited : true ,
289+ Exited : ! info . State . Running ,
270290 ExitCode : info .State .ExitCode ,
271291 OOMKilled : info .State .OOMKilled ,
272292 }, nil
0 commit comments