Skip to content

Commit 9358ef8

Browse files
authored
fix(internal): catch possible OSError when calling platform.libc_ver() (backport #4828) (#4832)
## Description When calling `platform.libc_ver()` on an unsupported system it can raise an `OSError`. This change adds exception handling for `OSError` and regression test. ## Reviewer Checklist - [x] Title is accurate. - [x] Description motivates each change. - [x] No unnecessary changes were introduced in this PR. - [x] Avoid breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [x] Tests provided or description of manual testing performed is included in the code or PR. - [x] Release note has been added and follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/contributing.html#Release-Note-Guidelines), or else `changelog/no-changelog` label added. - [x] All relevant GitHub issues are correctly linked. - [x] Backports are identified and tagged with Mergifyio.
1 parent 5a7debf commit 9358ef8

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

ddtrace/internal/telemetry/data.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,28 @@ def _get_container_id():
3333
def _get_os_version():
3434
# type: () -> str
3535
"""Returns the os version for applications running on Unix, Mac or Windows 32-bit"""
36-
mver, _, _ = platform.mac_ver()
37-
_, wver, _, _ = platform.win32_ver()
38-
_, lver = platform.libc_ver()
36+
try:
37+
mver, _, _ = platform.mac_ver()
38+
if mver:
39+
return mver
40+
41+
_, wver, _, _ = platform.win32_ver()
42+
if wver:
43+
return wver
44+
45+
# This is the call which is more likely to fail
46+
#
47+
# https://docs.python.org/3/library/platform.html#unix-platforms
48+
# Note that this function has intimate knowledge of how different libc versions add symbols
49+
# to the executable is probably only usable for executables compiled using gcc.
50+
_, lver = platform.libc_ver()
51+
if lver:
52+
return lver
53+
except OSError:
54+
# We were unable to lookup the proper version
55+
pass
3956

40-
return mver or wver or lver or ""
57+
return ""
4158

4259

4360
@cached()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
telemetry: This fix resolves an issue when we try to fetch ``platform.libc_ver()`` on an unsupported system.

tests/telemetry/test_data.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ def test_get_host_info():
108108
@pytest.mark.parametrize(
109109
"mac_ver,win32_ver,libc_ver,expected",
110110
[
111-
((None, None, None), (None, "4.1.6", None, None), (None, None), "4.1.6"),
112-
(("3.5.6", None, None), (None, "", None, None), (None, None), "3.5.6"),
113-
((None, None, None), (None, None, None, None), (None, "1.2.7"), "1.2.7"),
114-
((None, None, None), (None, None, None, None), (None, None), ""),
111+
(("", "", ""), ("", "4.1.6", "", ""), ("", ""), "4.1.6"),
112+
(("3.5.6", "", ""), ("", "", "", ""), ("", ""), "3.5.6"),
113+
(("", "", ""), ("", "", "", ""), ("", "1.2.7"), "1.2.7"),
114+
(("", "", ""), ("", "", "", ""), ("", ""), ""),
115115
],
116116
)
117117
def test_get_os_version(mac_ver, win32_ver, libc_ver, expected):
@@ -125,6 +125,32 @@ def test_get_os_version(mac_ver, win32_ver, libc_ver, expected):
125125
assert _get_os_version() == expected
126126

127127

128+
@pytest.mark.parametrize(
129+
"mac_ver,win32_ver,expected",
130+
[
131+
# We are on a unix system that raised an OSError
132+
(("", "", ""), ("", "", "", ""), ""),
133+
# We are on macOS, we never call platform.libc_ver(), so success
134+
(("3.5.6", "", ""), ("", "", "", ""), "3.5.6"),
135+
# We are on a Windows machine, we never call platform.libc_ver(), so success
136+
(("", "", ""), ("", "4.1.6", "", ""), "4.1.6"),
137+
],
138+
)
139+
def test_get_os_version_os_error(mac_ver, win32_ver, expected):
140+
"""regression test for platform.libc_ver() raising an OSError on Windows/unsupported machine"""
141+
with mock.patch("platform.mac_ver") as macos:
142+
macos.return_value = mac_ver
143+
144+
with mock.patch("platform.win32_ver") as win32:
145+
win32.return_value = win32_ver
146+
147+
# Cause platform.libc_ver() to raise an OSError
148+
with mock.patch("platform.libc_ver") as libc:
149+
libc.side_effect = OSError
150+
151+
assert _get_os_version() == expected
152+
153+
128154
def test_get_container_id_when_container_exists():
129155
"""
130156
validates the return value of _get_container_id when get_container_info()

0 commit comments

Comments
 (0)