Skip to content

Commit c6b4289

Browse files
proc.readEnv() improvements
- Minimize the risk of race conditions when we're prompting the user to allow/deny a connection, while we're still reading proc's environ file. (this was actually a leak). - Preallocate the Env map with the expected environ vars.
1 parent 6ba7265 commit c6b4289

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

daemon/procmon/details.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ func (p *Process) ReadEnv() {
174174
}
175175
raw = bytes.Trim(raw, "\r\n\t")
176176
vars := strings.Split(string(raw), "\x00")
177+
env := make(map[string]string, len(vars))
177178
for _, s := range vars {
178179
idx := strings.Index(s, "=")
179180
if idx == -1 {
@@ -182,10 +183,14 @@ func (p *Process) ReadEnv() {
182183

183184
key := s[:idx]
184185
val := s[idx+1 : len(s)]
185-
p.mu.Lock()
186-
p.Env[key] = val
187-
p.mu.Unlock()
186+
env[key] = val
188187
}
188+
// Minimize the risk of race conditions by not locking the map inside the loop.
189+
// It may cause leaks when prompting the user to allow/deny.
190+
p.mu.Lock()
191+
p.Env = env
192+
p.mu.Unlock()
193+
189194
}
190195

191196
// ReadMaps reads the /proc/<pid>/maps file.

0 commit comments

Comments
 (0)