Skip to content

fix(linux): handle individual window errors in openWindows#198

Merged
sindresorhus merged 1 commit into
sindresorhus:mainfrom
RealmX1:fix/linux-openwindows-error-handling
Aug 27, 2025
Merged

fix(linux): handle individual window errors in openWindows#198
sindresorhus merged 1 commit into
sindresorhus:mainfrom
RealmX1:fix/linux-openwindows-error-handling

Conversation

@RealmX1

@RealmX1 RealmX1 commented Aug 26, 2025

Copy link
Copy Markdown
Contributor

Problem

The openWindows() function on Linux returns undefined when any single window fails to retrieve information. This causes a complete loss of window tracking functionality even when most windows are accessible.

Root Cause

The issue occurs in lib/linux.js at line 162 where the for await loop doesn't handle errors for individual windows. When getWindowInformation() throws an error for any window (common with zombie windows or permission issues), the entire function fails.

Solution

This PR adds try-catch blocks around individual getWindowInformation() calls, allowing the function to:

  • Continue processing other windows when one fails
  • Return a list of accessible windows
  • Only return undefined when no windows can be accessed

Changes

  • ✅ Added error handling for individual windows in openWindows()
  • ✅ Added error handling for individual windows in openWindowsSync()
  • ✅ Added optional debug logging via DEBUG_GET_WINDOWS environment variable
  • ✅ Maintains backward compatibility

Testing

  • Tested on Ubuntu 22.04 with X11
  • Verified behavior with zombie windows (processes terminated but window IDs remain)
  • Confirmed that valid windows are returned when some windows fail
  • No performance impact for normal operation

Test output showing the fix working:

Testing openWindows fix...
[get-windows] Failed to get information for window  0x6e00004: Unterminated string in JSON at position 32
[get-windows] Failed to get information for window  0xac00004: Cannot read properties of undefined
[get-windows] Successfully retrieved 17 windows, failed for 2 windows
Found 17 windows
Active window: Claude Code - get-windows - Cursor
✅ Fix working: openWindows returned valid list

Breaking Changes

None. The function signature and return behavior remain the same for normal operation.

Checklist

  • Code follows the project's style guidelines
  • Tests pass locally
  • Backward compatibility maintained
  • No unnecessary console output in production

This fix aligns the Linux implementation with the Windows C++ implementation, which already handles individual window errors gracefully.

🤖 Generated with Claude Code

When a window's information cannot be retrieved (e.g., zombie windows,
permission issues), openWindows() now continues processing other windows
instead of returning undefined for the entire list.

This fixes a critical issue where a single inaccessible window would
prevent all window information from being retrieved on Linux systems.

The fix adds:
- Try-catch blocks around individual getWindowInformation() calls
- Debug logging via DEBUG_GET_WINDOWS environment variable
- Graceful degradation returning partial window lists
- Similar error handling for both async and sync versions

This aligns the Linux implementation with the Windows C++ implementation
which already handles individual window errors gracefully.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@RealmX1 RealmX1 force-pushed the fix/linux-openwindows-error-handling branch from 6a15be8 to 18e904b Compare August 26, 2025 16:59
@sindresorhus sindresorhus merged commit 8be5b71 into sindresorhus:main Aug 27, 2025
1 check passed
@sindresorhus

Copy link
Copy Markdown
Owner

Can you try out the latest main branch on Linux before I release it?

https://github.com/sindresorhus/get-windows/commits/main/

@RealmX1

RealmX1 commented Aug 28, 2025

Copy link
Copy Markdown
Contributor Author

Test Report: Latest main branch on Linux

I've tested the latest main branch with the merged PR on Linux. The fix is working perfectly! 🎉

Test Environment

  • OS: Linux x64
  • Node: v22.14.0

Comparison Results

Before (v9.2.0):

  • activeWindow(): ✅ Works
  • openWindows(): ❌ Returns undefined when any window fails

After (latest main):

  • activeWindow(): ✅ Works
  • openWindows(): ✅ Successfully returns 22 windows despite 1 failure
  • Debug output shows: [get-windows] Successfully retrieved 22 windows, failed for 1 windows

The error handling is now resilient - individual window failures don't break the entire function. The debug logging also provides great visibility when enabled.

Conclusion: The fix works as expected and is ready for release. Thank you for merging this!

@RealmX1

RealmX1 commented Aug 28, 2025

Copy link
Copy Markdown
Contributor Author

I might not have followed all your instructions in PR though. just read it. If you don't want the console.debug logs just remove them.

@RealmX1

RealmX1 commented Aug 28, 2025

Copy link
Copy Markdown
Contributor Author

also, the reason for failed retrieval might be becuase zombie windows in linux which persist in system even after closed. I don't know how that can be addressed. This is just a workaround the problem by adding error handling. but I guess fixing that isn't the responsibility of this package so I dind't bother investigating it.

@sindresorhus

Copy link
Copy Markdown
Owner

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.

2 participants