go-prompt
go-prompt copied to clipboard
Fix file descriptor leak
Fixes file descriptor leak as described in #253
Reproduced by modifying _example/simple-echo/main.go
, looping the body of the main function in order to observe multiple calls to prompt.Input()
. "Exit" added for convenience:
diff --git a/_example/simple-echo/main.go b/_example/simple-echo/main.go
index 12b1bb4..b02199f 100644
--- a/_example/simple-echo/main.go
+++ b/_example/simple-echo/main.go
@@ -12,17 +12,23 @@ func completer(in prompt.Document) []prompt.Suggest {
{Text: "articles", Description: "Store the article text posted by user"},
{Text: "comments", Description: "Store the text commented to articles"},
{Text: "groups", Description: "Combine users with specific rules"},
+ {Text: "exit", Description: "Stop the loop"},
}
return prompt.FilterHasPrefix(s, in.GetWordBeforeCursor(), true)
}
func main() {
- in := prompt.Input(">>> ", completer,
- prompt.OptionTitle("sql-prompt"),
- prompt.OptionHistory([]string{"SELECT * FROM users;"}),
- prompt.OptionPrefixTextColor(prompt.Yellow),
- prompt.OptionPreviewSuggestionTextColor(prompt.Blue),
- prompt.OptionSelectedSuggestionBGColor(prompt.LightGray),
- prompt.OptionSuggestionBGColor(prompt.DarkGray))
- fmt.Println("Your input: " + in)
+ for {
+ in := prompt.Input(">>> ", completer,
+ prompt.OptionTitle("sql-prompt"),
+ prompt.OptionHistory([]string{"SELECT * FROM users;"}),
+ prompt.OptionPrefixTextColor(prompt.Yellow),
+ prompt.OptionPreviewSuggestionTextColor(prompt.Blue),
+ prompt.OptionSelectedSuggestionBGColor(prompt.LightGray),
+ prompt.OptionSuggestionBGColor(prompt.DarkGray))
+ if in == "exit" {
+ break
+ }
+ fmt.Println("Your input: " + in)
+ }
}
Each call to Input will leak another file descriptor, seen with lsof -p <GO PID> | grep 'dev\/tty' | wc -l
:
Here you can see the count of /dev/tty file descriptors increasing:
Calling syscall.Close(t.fd)
prevents this leak. After this fix the count of file descriptors is constant: