Skip to content

Tolerate stream metadata that is not valid UTF-8 - #117

Merged
cboulay merged 4 commits into
labstreaminglayer:mainfrom
adityasingh2400:fix/non-utf8-metadata-strings
Sep 20, 2026
Merged

cboulay merged 4 commits into
labstreaminglayer:mainfrom
adityasingh2400:fix/non-utf8-metadata-strings

Conversation

@adityasingh2400

Copy link
Copy Markdown
Contributor

StreamInfo.hostname() raises UnicodeDecodeError on Windows machines whose hostname contains accented characters, as reported in #69. as_xml() fails in the same way because the hostname is embedded in the info document, so a resolver loop can die simply from discovering a stream that lives on such a machine.

The root cause is that every string coming back from liblsl was run through a strict bytes.decode("utf-8"). That is safe for values pylsl itself wrote, since pylsl encodes them as UTF-8 on the way in, but the hostname is read from the operating system and Windows hands it back in the active ANSI code page. The same applies to any metadata written by a non-Python LSL application running on such a machine.

This adds a small _to_str helper in pylsl.util that tries UTF-8 first and falls back to latin-1. The fallback cannot fail, keeps every original byte so a caller can re-encode and decode with the correct code page if it knows better, and renders the common accented-Latin case correctly, which is the fix the reporter validated in the issue thread. The helper is applied to the metadata accessors in info.py: name, type, source_id, uid, session_id, hostname, as_xml, and the XMLElement name, value, and child_value getters. The sample data path in inlet.py is deliberately left alone, since raw bytes in cf_string channels are a separate discussion in #54.

Tested with a new test/test_encoding.py that exercises the helper directly and simulates a non-UTF-8 platform by monkeypatching the relevant liblsl calls to return cp1252 bytes. With the tests in place but info.py reverted, three of them fail with exactly the error from the issue, UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe1 in position 7. With the change applied the whole suite passes, 18 tests against liblsl 1.17.7 on macOS with Python 3.14. ruff check and ruff format --check report nothing new relative to main.

Fixes #69

adityasingh2400 and others added 3 commits August 5, 2026 05:03
Every string returned by liblsl was decoded with a strict UTF-8 decode.
The hostname is read from the operating system, and Windows returns it in
the active ANSI code page, so a machine whose name contains accented
characters made StreamInfo.hostname() and StreamInfo.as_xml() raise
UnicodeDecodeError.

Add a _to_str helper that tries UTF-8 and falls back to latin-1, which
cannot fail and preserves the original bytes, and use it for the metadata
accessors in info.py.

Fixes labstreaminglayer#69
reset_uid() landed on main (labstreaminglayer#115) after this branch was written and
still used a strict UTF-8 decode. Route it through _to_str for
consistency with the other metadata accessors, and add a test.
@cboulay
cboulay merged commit f26f725 into labstreaminglayer:main Sep 20, 2026
19 checks passed
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.

Stream decodification Issue on Windows Machines with Accentuated Hostnames

2 participants