Commit 1a56737
fix: move defer out of retry loop in waitForJob (#308)
## Problem
The `waitForJob` function had a `defer closer.Close()` inside a `for`
loop. Go defers execute when the *function* returns, not when the loop
iteration ends. This means:
1. Each retry iteration adds a new deferred close that only runs when
the function returns, not at the end of each iteration.
2. On retries, multiple closers accumulate unnecessarily.
Note: In v0.0.12, this was also a nil pointer dereference because `defer
closer.Close()` was called *before* checking the error from
`TemplateVersionLogsAfter`, causing a panic when `closer` was nil. That
ordering was fixed in v0.0.13, but the defer-in-loop issue remained.
## Fix
Extract the loop body into a separate `waitForJobOnce` function so the
`defer` executes properly at the end of each attempt, closing the log
stream before retrying.
## Context
This was observed as a SIGSEGV panic in CI when using
`terraform-provider-coderd` v0.0.12 in the `coder/coder` dogfood
workflow:
```
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x1000fcd]
goroutine 41 [running]:
github.com/coder/terraform-provider-coderd/internal/provider.waitForJob(...)
github.com/coder/terraform-provider-coderd/internal/provider/template_resource.go:1086 +0x1ad
```
---------
Co-authored-by: blink-so[bot] <211532188+blink-so[bot]@users.noreply.github.com>
Co-authored-by: Ethan Dickson <ethanndickson@gmail.com>1 parent 7bd3d02 commit 1a56737
4 files changed
Lines changed: 315 additions & 40 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
8 | 10 | | |
9 | 11 | | |
10 | 12 | | |
| |||
63 | 65 | | |
64 | 66 | | |
65 | 67 | | |
66 | | - | |
67 | 68 | | |
68 | 69 | | |
69 | 70 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
121 | | - | |
122 | | - | |
| 121 | + | |
| 122 | + | |
123 | 123 | | |
124 | 124 | | |
| 125 | + | |
| 126 | + | |
125 | 127 | | |
126 | 128 | | |
127 | 129 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| 17 | + | |
16 | 18 | | |
17 | 19 | | |
18 | 20 | | |
| |||
1104 | 1106 | | |
1105 | 1107 | | |
1106 | 1108 | | |
1107 | | - | |
1108 | | - | |
1109 | | - | |
| 1109 | + | |
| 1110 | + | |
| 1111 | + | |
| 1112 | + | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
| 1117 | + | |
1110 | 1118 | | |
1111 | | - | |
| 1119 | + | |
1112 | 1120 | | |
1113 | | - | |
1114 | | - | |
1115 | | - | |
1116 | | - | |
1117 | | - | |
1118 | | - | |
1119 | | - | |
1120 | | - | |
1121 | | - | |
1122 | | - | |
1123 | | - | |
1124 | | - | |
1125 | | - | |
1126 | | - | |
1127 | | - | |
1128 | | - | |
1129 | | - | |
1130 | | - | |
1131 | | - | |
1132 | | - | |
1133 | | - | |
1134 | | - | |
| 1121 | + | |
| 1122 | + | |
1135 | 1123 | | |
1136 | | - | |
1137 | | - | |
1138 | | - | |
| 1124 | + | |
| 1125 | + | |
| 1126 | + | |
| 1127 | + | |
| 1128 | + | |
| 1129 | + | |
| 1130 | + | |
| 1131 | + | |
| 1132 | + | |
| 1133 | + | |
| 1134 | + | |
| 1135 | + | |
| 1136 | + | |
| 1137 | + | |
| 1138 | + | |
| 1139 | + | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
1139 | 1143 | | |
1140 | | - | |
1141 | | - | |
1142 | | - | |
| 1144 | + | |
| 1145 | + | |
| 1146 | + | |
| 1147 | + | |
| 1148 | + | |
| 1149 | + | |
1143 | 1150 | | |
1144 | | - | |
1145 | | - | |
| 1151 | + | |
| 1152 | + | |
| 1153 | + | |
| 1154 | + | |
| 1155 | + | |
| 1156 | + | |
| 1157 | + | |
| 1158 | + | |
| 1159 | + | |
1146 | 1160 | | |
1147 | | - | |
1148 | 1161 | | |
1149 | | - | |
| 1162 | + | |
| 1163 | + | |
| 1164 | + | |
| 1165 | + | |
| 1166 | + | |
| 1167 | + | |
| 1168 | + | |
| 1169 | + | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
1150 | 1173 | | |
1151 | 1174 | | |
1152 | 1175 | | |
| |||
0 commit comments