Fix serial port resource leak in GTU7 sensor#18
Merged
Conversation
Co-authored-by: rustyeddy <2903425+rustyeddy@users.noreply.github.com>
Co-authored-by: rustyeddy <2903425+rustyeddy@users.noreply.github.com>
Co-authored-by: rustyeddy <2903425+rustyeddy@users.noreply.github.com>
Co-authored-by: rustyeddy <2903425+rustyeddy@users.noreply.github.com>
Contributor
Author
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Copilot
AI
changed the title
[WIP] Address feedback on Gtu7 sensor and serial drivers implementation
Fix serial port resource leak in GTU7 sensor
Jan 16, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request fixes a resource leak in the GTU7 GPS sensor where the serial port was opened but never closed. The fix adds proper cleanup by deferring the close operation and changes the reader field type to retain access to the Close method.
Changes:
- Changed GTU7's reader field from
io.Readertoio.ReadWriteCloserto enable cleanup - Added deferred cleanup in
Run()method to close the serial port on exit - Introduced
nopCloserwrapper to maintain test compatibility with readers that don't implement Close
Comments suppressed due to low confidence (1)
sensors/gtu7.go:73
- The code that checks and defaults the
cfg.Buffield (previously at lines 55-58) has been accidentally removed. This breaks the buffer size defaulting behavior that is tested ingtu7_test.go. Whencfg.Bufis 0 or negative, the tests expect it to default to 16, but now it will create a channel with capacity 0, causing the tests to fail.
var r io.ReadWriteCloser
if cfg.Reader != nil {
// Wrap test reader in a nopCloser if it doesn't implement ReadWriteCloser
if rc, ok := cfg.Reader.(io.ReadWriteCloser); ok {
r = rc
} else {
r = nopCloser{cfg.Reader}
}
} else {
port, err := cfg.Factory.OpenSerial(cfg.Serial)
if err != nil {
panic(err)
}
r = port
}
return >U7{
name: cfg.Name,
out: make(chan GPSFix, cfg.Buf),
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: rustyeddy <2903425+rustyeddy@users.noreply.github.com>
Co-authored-by: rustyeddy <2903425+rustyeddy@users.noreply.github.com>
rustyeddy
approved these changes
Jan 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The GTU7
Runmethod opens a serial port but never closes it, leaking file descriptors.Changes
io.Reader→io.ReadCloserto retainClose()access while reflecting actual usage (GTU7 only reads, never writes)Runexit, matching VH400's ADC cleanup patternio.NopCloserinstead of custom implementationThe
io.NopCloserwrapper from the standard library allowsstrings.NewReaderand similar test fixtures to work unchanged while production code usingSerialPortgets proper cleanup. Usingio.ReadCloserinstead ofio.ReadWriteClosermakes the intent clearer and reduces unnecessary custom code.✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.