Skip to content

Commit 3775a72

Browse files
authored
fix PAPI failure to stop on reload (#3679)
1 parent 5fc0b2a commit 3775a72

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

pkg/apiserver/papi.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ type Papi struct {
7070
consoleConfig *csconfig.ConsoleConfig
7171
Logger *log.Entry
7272
apic *apic
73+
stopChan chan struct{}
7374
}
7475

7576
type PapiPermCheckError struct {
@@ -118,6 +119,7 @@ func NewPAPI(apic *apic, dbClient *database.Client, consoleConfig *csconfig.Cons
118119
apic: apic,
119120
consoleConfig: consoleConfig,
120121
Logger: logger.WithFields(log.Fields{"interval": SyncInterval.Seconds(), "source": "papi"}),
122+
stopChan: make(chan struct{}),
121123
}
122124

123125
return papi, nil
@@ -316,8 +318,11 @@ func (p *Papi) Pull(ctx context.Context) error {
316318
}
317319

318320
logger.Debugf("set last timestamp to %s", newTime)
321+
case <-p.stopChan:
322+
cancel()
319323
}
320324
}
325+
321326
}
322327

323328
func (p *Papi) SyncDecisions(ctx context.Context) error {
@@ -398,5 +403,9 @@ func (p *Papi) SendDeletedDecisions(ctx context.Context, cacheOrig *models.Decis
398403
func (p *Papi) Shutdown() {
399404
p.Logger.Infof("Shutting down PAPI")
400405
p.syncTomb.Kill(nil)
406+
select {
407+
case p.stopChan <- struct{}{}: // Cancel any HTTP request still in progress
408+
default:
409+
}
401410
p.Client.Stop()
402411
}

pkg/longpollclient/client.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ func (c *LongPollClient) doQuery(ctx context.Context) (*http.Response, error) {
6868
req.Header.Set("Accept", "application/json")
6969
resp, err := c.httpClient.Do(req)
7070
if err != nil {
71+
if errors.Is(err, context.Canceled) {
72+
return nil, ctx.Err() // Don't log
73+
}
7174
logger.Errorf("failed to execute request: %s", err)
7275
return nil, err
7376
}
@@ -175,6 +178,10 @@ func (c *LongPollClient) pollEvents(ctx context.Context) error {
175178
close(c.c)
176179
return err
177180
}
181+
if errors.Is(err, context.Canceled) {
182+
c.logger.Debug("context canceled, stopping polling")
183+
return nil
184+
}
178185
c.logger.Errorf("failed to poll: %s, retrying in %s", err, currentBackoff)
179186
select {
180187
case <-c.t.Dying():

0 commit comments

Comments
 (0)