Skip to content

Commit

Permalink
Fix dangling FDs on Linux platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
newAM authored Apr 22, 2023
1 parent f7905df commit 1a8ece5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html),
as of version 2.1.1.

## [3.0.3] - 2023-04-22
### Fixed
- Fixed dangling file descriptors on linux platforms after calling `get_monitors()`.

## [3.0.2] - 2022-11-05
### Added
- Added support for pyudev version 0.24.0.
Expand Down Expand Up @@ -78,7 +82,8 @@ as of version 2.1.1.
- Added a changelog.


[Unreleased]: https://github.com/newAM/monitorcontrol/compare/3.0.2...HEAD
[Unreleased]: https://github.com/newAM/monitorcontrol/compare/3.0.3...HEAD
[3.0.3]: https://github.com/newAM/monitorcontrol/compare/3.0.2...3.0.3
[3.0.2]: https://github.com/newAM/monitorcontrol/compare/3.0.1...3.0.2
[3.0.1]: https://github.com/newAM/monitorcontrol/compare/3.0.0...3.0.1
[3.0.0]: https://github.com/newAM/monitorcontrol/compare/2.5.1...3.0.0
Expand Down
15 changes: 13 additions & 2 deletions monitorcontrol/vcp/vcp_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,31 @@ def __init__(self, bus_number: int):
self.logger = logging.getLogger(__name__)
self.bus_number = bus_number
self.fd: Optional[str] = None
self.fp = None
self.fp: str = f"/dev/i2c-{self.bus_number}"
# time of last feature set call
self.last_set: Optional[float] = None

def __enter__(self):
def cleanup(fd: Optional[int]):
if fd is not None:
try:
os.close(self.fd)
except OSError:
pass

try:
self.fp = f"/dev/i2c-{self.bus_number}"
self.fd = os.open(self.fp, os.O_RDWR)
fcntl.ioctl(self.fd, self.I2C_SLAVE, self.DDCCI_ADDR)
self.read_bytes(1)
except PermissionError as e:
cleanup(self.fd)
raise VCPPermissionError(f"permission error for {self.fp}") from e
except OSError as e:
cleanup(self.fd)
raise VCPIOError(f"unable to open VCP at {self.fp}") from e
except Exception as e:
cleanup(self.fd)
raise e
return self

def __exit__(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ target-version = ["py36", "py37", "py38"]
[tool.poetry]
name = "monitorcontrol"
description = "Monitor controls using MCCS over DDC-CI."
version = "3.0.2"
version = "3.0.3"
authors = ["Alex Martens <[email protected]>"]
license = "MIT"
readme = "README.rst"
Expand Down

0 comments on commit 1a8ece5

Please sign in to comment.