Skip to content

Fix serial port resource leak in GTU7 sensor#18

Merged
rustyeddy merged 8 commits intogtu7from
copilot/sub-pr-16-again
Jan 17, 2026
Merged

Fix serial port resource leak in GTU7 sensor#18
rustyeddy merged 8 commits intogtu7from
copilot/sub-pr-16-again

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 16, 2026

The GTU7 Run method opens a serial port but never closes it, leaking file descriptors.

Changes

  • Changed GTU7.r field type: io.Readerio.ReadCloser to retain Close() access while reflecting actual usage (GTU7 only reads, never writes)
  • Added cleanup defer: Close serial port on Run exit, matching VH400's ADC cleanup pattern
  • Test compatibility: Wrap test readers with stdlib io.NopCloser instead of custom implementation
func (g *GTU7) Run(ctx context.Context) error {
    defer close(g.out)
    defer func() {
        _ = g.r.Close()
    }()
    // ... rest of implementation
}

The io.NopCloser wrapper from the standard library allows strings.NewReader and similar test fixtures to work unchanged while production code using SerialPort gets proper cleanup. Using io.ReadCloser instead of io.ReadWriteCloser makes 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.

Copilot AI and others added 4 commits January 16, 2026 17:39
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>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jan 16, 2026

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:

  • go.googlesource.com
    • Triggering command: /update-job-proxy /update-job-proxy internal/sync -lang=go1.24 ux_amd64/compile--log-level -o x64/src/net -trimpath cal/bin/as -p runtime/trace -lang=go1.24 o_main.o --gd�� submodules | head -n 10 6161922/b155/ /x86_64-linux-gnu/13/cc1 /tmp/ccEeTN1E.s -nolocalimports -importcfg /x86_64-linux-gnu/13/cc1 (dns block)
    • Triggering command: /update-job-proxy /update-job-proxy -o br-d09db63bdca7 -j DROP thority_2017.pem bash -store-immutable --file /home/dependabot/dependabot-updater/git.store --noprofile x64/src/internalREDACTED ache/go/1.24.11/REDACTED test -e e6bd08fe64d74a3eaff8dbdef6603dac225370/shallow.lock ache/go/1.24.11/x64/pkg/tool/lin/usr/share/ca-certificates/mozilla/GDCA_TrustAUTH_R5_ROOT.crt (dns block)
  • gopkg.in
    • Triggering command: /update-job-proxy /update-job-proxy internal/sync -lang=go1.24 ux_amd64/compile--log-level -o x64/src/net -trimpath cal/bin/as -p runtime/trace -lang=go1.24 o_main.o --gd�� submodules | head -n 10 6161922/b155/ /x86_64-linux-gnu/13/cc1 /tmp/ccEeTN1E.s -nolocalimports -importcfg /x86_64-linux-gnu/13/cc1 (dns block)
    • Triggering command: /update-job-proxy /update-job-proxy -o br-d09db63bdca7 -j DROP thority_2017.pem bash -store-immutable --file /home/dependabot/dependabot-updater/git.store --noprofile x64/src/internalREDACTED ache/go/1.24.11/REDACTED test -e e6bd08fe64d74a3eaff8dbdef6603dac225370/shallow.lock ache/go/1.24.11/x64/pkg/tool/lin/usr/share/ca-certificates/mozilla/GDCA_TrustAUTH_R5_ROOT.crt (dns block)
  • periph.io
    • Triggering command: /update-job-proxy /update-job-proxy internal/sync -lang=go1.24 ux_amd64/compile--log-level -o x64/src/net -trimpath cal/bin/as -p runtime/trace -lang=go1.24 o_main.o --gd�� submodules | head -n 10 6161922/b155/ /x86_64-linux-gnu/13/cc1 /tmp/ccEeTN1E.s -nolocalimports -importcfg /x86_64-linux-gnu/13/cc1 (dns block)
    • Triggering command: /update-job-proxy /update-job-proxy -o br-d09db63bdca7 -j DROP thority_2017.pem bash -store-immutable --file /home/dependabot/dependabot-updater/git.store --noprofile x64/src/internalREDACTED ache/go/1.24.11/REDACTED test -e e6bd08fe64d74a3eaff8dbdef6603dac225370/shallow.lock ache/go/1.24.11/x64/pkg/tool/lin/usr/share/ca-certificates/mozilla/GDCA_TrustAUTH_R5_ROOT.crt (dns block)

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
Copilot AI requested a review from rustyeddy January 16, 2026 17:43
@rustyeddy rustyeddy marked this pull request as ready for review January 17, 2026 15:53
Copilot AI review requested due to automatic review settings January 17, 2026 15:53
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.Reader to io.ReadWriteCloser to enable cleanup
  • Added deferred cleanup in Run() method to close the serial port on exit
  • Introduced nopCloser wrapper 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.Buf field (previously at lines 55-58) has been accidentally removed. This breaks the buffer size defaulting behavior that is tested in gtu7_test.go. When cfg.Buf is 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 &GTU7{
		name: cfg.Name,
		out:  make(chan GPSFix, cfg.Buf),

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread sensors/gtu7.go Outdated
Copilot AI and others added 2 commits January 17, 2026 16:00
Co-authored-by: rustyeddy <2903425+rustyeddy@users.noreply.github.com>
Co-authored-by: rustyeddy <2903425+rustyeddy@users.noreply.github.com>
@rustyeddy rustyeddy merged commit 6e9edf2 into gtu7 Jan 17, 2026
@rustyeddy rustyeddy deleted the copilot/sub-pr-16-again branch January 17, 2026 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants