@@ -79,15 +79,14 @@ type API struct {
79
79
containersErr error // Error from the last list operation.
80
80
devcontainerNames map [string ]bool // By devcontainer name.
81
81
knownDevcontainers map [string ]codersdk.WorkspaceAgentDevcontainer // By workspace folder.
82
+ devcontainerLogSourceIDs map [string ]uuid.UUID // By workspace folder.
82
83
configFileModifiedTimes map [string ]time.Time // By config file path.
83
84
recreateSuccessTimes map [string ]time.Time // By workspace folder.
84
85
recreateErrorTimes map [string ]time.Time // By workspace folder.
85
86
injectedSubAgentProcs map [string ]subAgentProcess // By workspace folder.
86
87
usingWorkspaceFolderName map [string ]bool // By workspace folder.
87
88
ignoredDevcontainers map [string ]bool // By workspace folder. Tracks three states (true, false and not checked).
88
89
asyncWg sync.WaitGroup
89
-
90
- devcontainerLogSourceIDs map [string ]uuid.UUID // By workspace folder.
91
90
}
92
91
93
92
type subAgentProcess struct {
@@ -530,7 +529,6 @@ func (api *API) updateContainers(ctx context.Context) error {
530
529
// will clear up on the next update.
531
530
if ! errors .Is (err , context .Canceled ) {
532
531
api .mu .Lock ()
533
- api .containers = codersdk.WorkspaceAgentListContainersResponse {}
534
532
api .containersErr = err
535
533
api .mu .Unlock ()
536
534
}
@@ -936,34 +934,33 @@ func (api *API) CreateDevcontainer(workspaceFolder, configPath string, opts ...D
936
934
return xerrors .Errorf ("devcontainer not found" )
937
935
}
938
936
939
- api .asyncWg .Add (1 )
940
- defer api .asyncWg .Done ()
941
- api .mu .Unlock ()
942
-
943
937
var (
944
- err error
945
938
ctx = api .ctx
946
939
logger = api .logger .With (
947
940
slog .F ("devcontainer_id" , dc .ID ),
948
941
slog .F ("devcontainer_name" , dc .Name ),
949
942
slog .F ("workspace_folder" , dc .WorkspaceFolder ),
950
- slog .F ("config_path" , configPath ),
943
+ slog .F ("config_path" , dc . ConfigPath ),
951
944
)
952
945
)
953
946
954
- if dc .ConfigPath != configPath {
955
- logger .Warn (ctx , "devcontainer config path mismatch" ,
956
- slog .F ("config_path_param" , configPath ),
957
- )
958
- }
959
-
960
947
// Send logs via agent logging facilities.
961
948
logSourceID := api .devcontainerLogSourceIDs [dc .WorkspaceFolder ]
962
949
if logSourceID == uuid .Nil {
963
- // Fallback to the external log source ID if not found.
950
+ api . logger . Debug ( api . ctx , "devcontainer log source ID not found, falling back to external log source ID" )
964
951
logSourceID = agentsdk .ExternalLogSourceID
965
952
}
966
953
954
+ api .asyncWg .Add (1 )
955
+ defer api .asyncWg .Done ()
956
+ api .mu .Unlock ()
957
+
958
+ if dc .ConfigPath != configPath {
959
+ logger .Warn (ctx , "devcontainer config path mismatch" ,
960
+ slog .F ("config_path_param" , configPath ),
961
+ )
962
+ }
963
+
967
964
scriptLogger := api .scriptLogger (logSourceID )
968
965
defer func () {
969
966
flushCtx , cancel := context .WithTimeout (api .ctx , 5 * time .Second )
@@ -982,7 +979,7 @@ func (api *API) CreateDevcontainer(workspaceFolder, configPath string, opts ...D
982
979
upOptions := []DevcontainerCLIUpOptions {WithUpOutput (infoW , errW )}
983
980
upOptions = append (upOptions , opts ... )
984
981
985
- _ , err = api .dccli .Up (ctx , dc .WorkspaceFolder , configPath , upOptions ... )
982
+ _ , err : = api .dccli .Up (ctx , dc .WorkspaceFolder , configPath , upOptions ... )
986
983
if err != nil {
987
984
// No need to log if the API is closing (context canceled), as this
988
985
// is expected behavior when the API is shutting down.
@@ -1002,15 +999,6 @@ func (api *API) CreateDevcontainer(workspaceFolder, configPath string, opts ...D
1002
999
1003
1000
logger .Info (ctx , "devcontainer created successfully" )
1004
1001
1005
- // Ensure the container list is updated immediately after creation.
1006
- // This makes sure that dc.Container is populated before we acquire
1007
- // the lock avoiding a temporary inconsistency in the API state
1008
- // where status is running, but the container is nil.
1009
- if err := api .RefreshContainers (ctx ); err != nil {
1010
- logger .Error (ctx , "failed to trigger immediate refresh after devcontainer creation" , slog .Error (err ))
1011
- return xerrors .Errorf ("refresh containers: %w" , err )
1012
- }
1013
-
1014
1002
api .mu .Lock ()
1015
1003
dc = api .knownDevcontainers [dc .WorkspaceFolder ]
1016
1004
// Update the devcontainer status to Running or Stopped based on the
@@ -1029,6 +1017,13 @@ func (api *API) CreateDevcontainer(workspaceFolder, configPath string, opts ...D
1029
1017
api .knownDevcontainers [dc .WorkspaceFolder ] = dc
1030
1018
api .mu .Unlock ()
1031
1019
1020
+ // Ensure an immediate refresh to accurately reflect the
1021
+ // devcontainer state after recreation.
1022
+ if err := api .RefreshContainers (ctx ); err != nil {
1023
+ logger .Error (ctx , "failed to trigger immediate refresh after devcontainer creation" , slog .Error (err ))
1024
+ return xerrors .Errorf ("refresh containers: %w" , err )
1025
+ }
1026
+
1032
1027
return nil
1033
1028
}
1034
1029
0 commit comments