Bug 482685 - trying to change active window signal connections in kwin script causes logs to be exponentially spammed by error message even though script works fine
Summary: trying to change active window signal connections in kwin script causes logs ...
Status: REPORTED
Alias: None
Product: kwin
Classification: Plasma
Component: scripting (show other bugs)
Version: 6.0.1
Platform: Arch Linux Linux
: NOR normal
Target Milestone: ---
Assignee: KWin default assignee
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-03-07 10:54 UTC by sarahvca52
Modified: 2024-03-07 22:54 UTC (History)
0 users

See Also:
Latest Commit:
Version Fixed In:
Sentry Crash Report:


Attachments
Rudimentary version of script (4.11 KB, application/zip)
2024-03-07 11:48 UTC, sarahvca52
Details

Note You need to log in before you can comment on or make changes to this bug.
Description sarahvca52 2024-03-07 10:54:12 UTC
SUMMARY
I would like to preface this by saying that i barely know javascript and dont have much experience with kwin scripting, but I was trying to stumble my way through porting a Plasma 5 kwin script to Plasma 6 and I came across this problem that's making it impossible to do so.

This script is designed to simulate macos-style fullscreening to a new desktop by detecting when the active window maximizes and sending it to a new desktop, and sending it back and/or closing the new virtual desktop when un-maximizing. So far I have that part working in its most basic capacity. Since I can't see a way to get a signal when this happens to any window unlike plasma 5, I have the script detect the active window upon starting and connect some of the windows signals to functions meant to detect when the window changes states and respond appropriately. 

the signal "workspace.windowActivated" is also connected to a function so that when window focus changes, the function re-detects the active window and connects its signals, allowing for the script to work regardless of the currently focused window.

Here's an excerpt from my script:

function install() { //attach to new window
    workspace.windowActivated.connect(install); //Changes active window whenever focus changes
    workspace.activeWindow.maximizedAboutToChange.connect(fullHandler); 
    workspace.activeWindow.fullScreenChanged.connect(fullHandler);
    workspace.windowAdded.connect(createdClient);
    oldActiveWindow = workspace.activeWindow;
    //log("Handler installed for " + workspace.activeWindow);
}

function uninstall() { //detach from old window
    oldActiveWindow.maximizedAboutToChange.disconnect(fullHandler);
    oldActiveWindow.fullScreenChanged.disconnect(fullHandler)
    workspace.windowAdded.disconnect(createdClient);
    //log("Handler cleared for " + oldActiveWindow);
    install();
}

When a new window is focused, it calls uninstall() which then calls install(). However, every time it does this the topmost function that calls on workspace.activeWindow will give me this error message in journald:

main.js:103: TypeError: Cannot read property [first activeWindow signal connected in install()] of null

As far as I can tell this error has no effect on the script or its functioning. It works just as I would expect. However every time a new window is focused this message gets printed to journald more and more often until focusing a new window maxes my hard drive and freezes my computer for several minutes until all of the lines are written. I've seen it write well over 500 lines of this same error message on one window change, only to write what seems to be well over 1000 next time i try to change focus.. No modification that I have been able to make to this script while still allowing it to function has fixed this. I've tried just removing the uninstall() function entirely to see if maybe that does anything but that seems to make the problem worse.

I piped journalctl -f | grep kwin to a file and turned on my script and changed window focus a few times before turning it off and ending the command. The file was 37000 lines long and all of it was just this message:

Mar 07 04:47:47 GE76-Raider-Arch kwin_wayland[197668]: file:///home/sarah/.local/share/kwin/scripts/macsimize/contents/code/main.js:103: TypeError: Cannot read property 'maximizedAboutToChange' of null

I don't have enough knowledge of kwin to know why this would be happening but this doesn't seem like its a problem with my script given that as far as I know I'm doing everything according to what's available on the API.


STEPS TO REPRODUCE
1. Create script that connects signals from active window and workspace.windowActivated
2. Have script re-detect active window when focus is changed

OBSERVED RESULT
This error will be printed, and then about 5 times as many will be printed the next time focus is changed until your computer crashes.

EXPECTED RESULT
I can disconnect from and reconnect to windows without spam to journald or harm to my ssd.

SOFTWARE/OS VERSIONS
Arch Linux x86_64
KDE Plasma 6.0.1
KDE Frameworks 6.0.0: 
Qt Version 6.6.2
Graphics Platform: Wayland
Comment 1 sarahvca52 2024-03-07 11:48:04 UTC
Created attachment 166572 [details]
Rudimentary version of script
Comment 2 sarahvca52 2024-03-07 22:54:23 UTC
After further messing with the script I've discovered that the problem isn't the error itself, its that the workspace.windowActivated signal itself is getting sent that many times to the program. For some reason the signal spams the script more and more after every window change.