Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8991a59

Browse files
authored
fix: wait for initial update before marking API as ready (#21363)
_Disclaimer: investigation done by Claude Opus 4.5_Closescoder/internal#1173Closescoder/internal#1174The agent containers API is only marked "ready" under this condition in`agent/agentcontainers/api.go`:```go// For now, all endpoints require the initial update to be done.// If we want to allow some endpoints to be available before// the initial update, we can enable this per-route.```However, what was actually being checked for was that the _init_ wasdone, not the _initial update_.In agent/agentcontainers/api.go, the `Start()` method: 1. Called `Init()` which closed `initDone` <--- API marked ready here 2. Then launched `go api.updaterLoop()` asynchronously3. `updaterLoop()` performs the initial container update <--- shouldhave marked it ready after thisThis PR fixes these semantics to avoid the race which was causing theabove two flakes.Signed-off-by: Danny Kopping <danny@coder.com>
1 parent72e478e commit8991a59

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

‎agent/agentcontainers/api.go‎

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ type API struct {
8787
agentDirectorystring
8888

8989
mu sync.RWMutex// Protects the following fields.
90-
initDonechanstruct{}// Closed by Init.
90+
initDonebool// Whether Init has been called.
91+
initialUpdateDonechanstruct{}// Closed after first updateContainers call in updaterLoop.
9192
updateChans []chanstruct{}
9293
closedbool
9394
containers codersdk.WorkspaceAgentListContainersResponse// Output from the last list operation.
@@ -325,7 +326,7 @@ func NewAPI(logger slog.Logger, options ...Option) *API {
325326
api:=&API{
326327
ctx:ctx,
327328
cancel:cancel,
328-
initDone:make(chanstruct{}),
329+
initialUpdateDone:make(chanstruct{}),
329330
updateTrigger:make(chanchanerror),
330331
updateInterval:defaultUpdateInterval,
331332
logger:logger,
@@ -379,20 +380,15 @@ func NewAPI(logger slog.Logger, options ...Option) *API {
379380
returnapi
380381
}
381382

382-
// Init applies a final set of options to the API andthen
383-
//closes initDone. This method can only be called once.
383+
// Init applies a final set of options to the API andmarks
384+
//initialization as done. This method can only be called once.
384385
func (api*API)Init(opts...Option) {
385386
api.mu.Lock()
386387
deferapi.mu.Unlock()
387-
ifapi.closed {
388-
return
389-
}
390-
select {
391-
case<-api.initDone:
388+
ifapi.closed||api.initDone {
392389
return
393-
default:
394390
}
395-
deferclose(api.initDone)
391+
api.initDone=true
396392

397393
for_,opt:=rangeopts {
398394
opt(api)
@@ -651,6 +647,7 @@ func (api *API) updaterLoop() {
651647
}else {
652648
api.logger.Debug(api.ctx,"initial containers update complete")
653649
}
650+
close(api.initialUpdateDone)
654651

655652
// We utilize a TickerFunc here instead of a regular Ticker so that
656653
// we can guarantee execution of the updateContainers method after
@@ -715,7 +712,7 @@ func (api *API) UpdateSubAgentClient(client SubAgentClient) {
715712
func (api*API)Routes() http.Handler {
716713
r:=chi.NewRouter()
717714

718-
ensureInitDoneMW:=func(next http.Handler) http.Handler {
715+
ensureInitialUpdateDoneMW:=func(next http.Handler) http.Handler {
719716
returnhttp.HandlerFunc(func(rw http.ResponseWriter,r*http.Request) {
720717
select {
721718
case<-api.ctx.Done():
@@ -726,8 +723,8 @@ func (api *API) Routes() http.Handler {
726723
return
727724
case<-r.Context().Done():
728725
return
729-
case<-api.initDone:
730-
//API init is done, we can start processing requests.
726+
case<-api.initialUpdateDone:
727+
//Initial update is done, we can start processing requests.
731728
}
732729
next.ServeHTTP(rw,r)
733730
})
@@ -736,7 +733,7 @@ func (api *API) Routes() http.Handler {
736733
// For now, all endpoints require the initial update to be done.
737734
// If we want to allow some endpoints to be available before
738735
// the initial update, we can enable this per-route.
739-
r.Use(ensureInitDoneMW)
736+
r.Use(ensureInitialUpdateDoneMW)
740737

741738
r.Get("/",api.handleList)
742739
r.Get("/watch",api.watchContainers)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp