fix: prevent IndexError in Type action parser and fix screenshot temp file leak#394
Open
Ricardo-M-L wants to merge 2 commits intozai-org:mainfrom
Open
fix: prevent IndexError in Type action parser and fix screenshot temp file leak#394Ricardo-M-L wants to merge 2 commits intozai-org:mainfrom
Ricardo-M-L wants to merge 2 commits intozai-org:mainfrom
Conversation
… file leak
1. handler.py parse_action(): split("text=", 1) can produce a single-
element list when the text parameter is missing from a malformed Type
action. Add a length check before accessing [1] and raise a clear
ValueError instead of an opaque IndexError.
2. adb/screenshot.py: temp files created with unique UUIDs are only
cleaned up on the success path. If Image.open() or img.save() raises,
the file leaks. Wrap the image processing in try/finally and also
clean up in the outer except block.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The HarmonyOS HDC path in phone_agent/hdc/screenshot.py had the identical leak pattern as the ADB path — if Image.open() or img.save() raised, the outer except swallowed the error and the temp file under $TMPDIR was never removed. Apply the same try/finally + except-path cleanup as ADB to guarantee the temp screenshot file is removed on every path. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Author
|
Pushed one more commit ( The fix mirrors the ADB one exactly: - img = Image.open(temp_path)
- ...
- os.remove(temp_path)
- return Screenshot(...)
- except Exception as e:
- print(f"Screenshot error: {e}")
- return _create_fallback_screenshot(...)
+ try:
+ img = Image.open(temp_path)
+ ...
+ return Screenshot(...)
+ finally:
+ if os.path.exists(temp_path):
+ os.remove(temp_path)
+ except Exception as e:
+ print(f"Screenshot error: {e}")
+ if os.path.exists(temp_path):
+ os.remove(temp_path)
+ return _create_fallback_screenshot(...)Happy to split this into a separate PR if you'd prefer — it keeps the scope narrow ("temp file leak" in both OS paths), so bundling felt natural, but let me know. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two bug fixes:
1. IndexError in
parse_action()for Type actions (handler.py:351)response.split("text=", 1)[1]assumes the response always containstext=. A malformed Type action likedo(action="Type")without a text parameter causesIndexError: list index out of range.Fix: Check split result length before accessing
[1], raise clearValueError.2. Temp file leak in ADB screenshot capture (
screenshot.py:65-85)Temp files created with UUID names are only cleaned up on the success path (
os.removeat line 77). IfImage.open(),img.save(), orbase64.b64encode()raises an exception, the temp file leaks in/tmp.Fix: Wrap image processing in
try/finallyto ensure cleanup regardless of exceptions.