@@ -353,7 +353,8 @@ type GPIOChip struct {
353
353
// The file descriptor to the Path device.
354
354
fd uintptr
355
355
// File associated with the file descriptor.
356
- file * os.File
356
+ file * os.File
357
+ osfile * os.File
357
358
}
358
359
359
360
func (chip * GPIOChip ) Name () string {
@@ -393,7 +394,9 @@ func newGPIOChip(path string) (*GPIOChip, error) {
393
394
}
394
395
chip .file = f
395
396
chip .fd = chip .file .Fd ()
396
- os .NewFile (uintptr (chip .fd ), "GPIO Chip - " + path )
397
+ // failure to maintain a reference leads to the file being garbage
398
+ // collected and the handle closed...
399
+ chip .osfile = os .NewFile (uintptr (chip .fd ), "GPIO Chip - " + path )
397
400
var info gpiochip_info
398
401
err = ioctl_gpiochip_info (chip .fd , & info )
399
402
if err != nil {
@@ -425,7 +428,10 @@ func newGPIOChip(path string) (*GPIOChip, error) {
425
428
// along with any configured Lines and LineSets.
426
429
func (chip * GPIOChip ) Close () {
427
430
_ = chip .file .Close ()
428
-
431
+ _ = chip .osfile .Close ()
432
+ chip .file = nil
433
+ chip .osfile = nil
434
+ chip .fd = 0
429
435
for _ , line := range chip .lines {
430
436
if line .fd != 0 {
431
437
line .Close ()
@@ -644,7 +650,9 @@ func (d *driverGPIO) Init() (bool, error) {
644
650
for _ , chip := range chips {
645
651
// On a pi, gpiochip0 is also symlinked to gpiochip4, checking the map
646
652
// ensures we don't duplicate the chip.
647
- if _ , found := mName [chip .Name ()]; ! found {
653
+ if _ , found := mName [chip .Name ()]; found {
654
+ chip .Close ()
655
+ } else {
648
656
Chips = append (Chips , chip )
649
657
mName [chip .Name ()] = struct {}{}
650
658
// Now, iterate over the lines on this chip.
0 commit comments