@@ -139,7 +139,7 @@ func (r *runner) Run(ctx context.Context) error {
139
139
140
140
cancel (fmt .Errorf ("await: %w" , err ))
141
141
142
- waitOrTimeout (r .stopTimeout , & waitCount )
142
+ err = waitOrTimeout (r .stopTimeout , & waitCount , err )
143
143
144
144
if errors .Is (err , context .Canceled ) {
145
145
slog .Info ("await: stopped on context canceled" )
@@ -151,18 +151,18 @@ func (r *runner) Run(ctx context.Context) error {
151
151
152
152
// waitTimeout will return either when the context is canceled or when the
153
153
// counter reaches 0. It will check the counter every 10ms.
154
- func waitOrTimeout (timeout time.Duration , counter * int32 ) {
155
- ticker := time .NewTicker (10 * time .Millisecond )
156
- for {
157
- select {
158
- case <- time .After (timeout ):
159
- slog .Error ("await: timed out waiting for subroutines to finish" )
160
- case <- ticker .C :
161
- if atomic .LoadInt32 (counter ) == 0 {
162
- return
163
- }
154
+ func waitOrTimeout (timeout time.Duration , counter * int32 , err error ) error {
155
+ slog .Info (fmt .Sprintf ("await: waiting %s for subroutines to finish" , timeout ))
156
+ for i := 0 * time .Second ; i < timeout ; i += 100 * time .Millisecond {
157
+ if atomic .LoadInt32 (counter ) == 0 {
158
+ return err
164
159
}
160
+ time .Sleep (100 * time .Millisecond )
161
+ }
162
+ if err != nil {
163
+ return fmt .Errorf ("await: shutdown timeout: %w" , err )
165
164
}
165
+ return nil
166
166
}
167
167
168
168
// ListenAndServe provides a graceful shutdown for an http.Server.
0 commit comments