Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SetFocus failed with exception #168

Open
tjsr opened this issue Feb 10, 2025 · 5 comments
Open

SetFocus failed with exception #168

tjsr opened this issue Feb 10, 2025 · 5 comments

Comments

@tjsr
Copy link

tjsr commented Feb 10, 2025

Logging for future investigation - have seen multiple of these.
Could be due to local changes - maybe not. We'll find out but logging so we have something to track later.

******************************************************************************
* wxPython Exception: 2025-02-11 10:09:16
******************************************************************************
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.3.1.1\plugins\python-ce\helpers\pydev\pydevd.py", line 2270, in <module>
    main()
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.3.1.1\plugins\python-ce\helpers\pydev\pydevd.py", line 2252, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.3.1.1\plugins\python-ce\helpers\pydev\pydevd.py", line 1563, in run
    return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.3.1.1\plugins\python-ce\helpers\pydev\pydevd.py", line 1570, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.3.1.1\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "H:\dev\CrossMgr\MainWin.py", line 4426, in <module>
    MainLoop()
  File "H:\dev\CrossMgr\MainWin.py", line 4423, in MainLoop
    app.MainLoop()
  File "H:\dev\CrossMgr\env\lib\site-packages\wx\core.py", line 2262, in MainLoop
    rv = wx.PyApp.MainLoop(self)
  File "H:\dev\CrossMgr\env\lib\site-packages\wx\core.py", line 2346, in Notify
    self.notify()
  File "H:\dev\CrossMgr\env\lib\site-packages\wx\core.py", line 3552, in Notify
    self.result = self.callable(*self.args, **self.kwargs)
  File "H:\dev\CrossMgr\LogPrintStackStderr.py", line 31, in DoLogRecord
    traceback.print_stack( file=sys.stderr )
6: ..\..\src\msw\window.cpp(595): 'SetFocus' failed with error 0x00000057 (The parameter is incorrect.).: <wx._core.LogRecordInfo object at 0x000001F5E949D900>
@tjsr
Copy link
Author

tjsr commented Feb 19, 2025

Managed to reproduce this with a bit more detail. I thought at first the issue seems could be something I've changed in the logging, and it not liking stderr being passed as a file param to print_stack - but it doesn't make a lot of sense why this would be the case. But on further investigation it looks like in os.py:326, there's use of a wx.MessageDialog as dlg statement, which then calls dlg.ShowModal()
I suspect what's happening is a background thread.sleep allows this to be dismissed, the file handle goes away, and ten something in here happens that the handle has now been destroyed - so SetFocus fails because that object has been destroyed.

`2025-02-19 18:32:08,520 Utils [INFO] call: openRace: "C:\Users\tim\OneDrive\Documents\2024-12-28-Test Event-r1-.cmn"


  • wxPython Exception: 2025-02-19 18:32:08

File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.3.1.1\plugins\python-ce\helpers\pydev\pydevd.py", line 2270, in
main()
File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.3.1.1\plugins\python-ce\helpers\pydev\pydevd.py", line 2252, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.3.1.1\plugins\python-ce\helpers\pydev\pydevd.py", line 1563, in run
return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.3.1.1\plugins\python-ce\helpers\pydev\pydevd.py", line 1570, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.3.1.1\plugins\python-ce\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "H:\dev\CrossMgr\MainWin.py", line 4564, in
MainLoop()
File "H:\dev\CrossMgr\MainWin.py", line 4561, in MainLoop
app.MainLoop()
File "H:\dev\CrossMgr\env\lib\site-packages\wx\core.py", line 2262, in MainLoop
rv = wx.PyApp.MainLoop(self)
File "H:\dev\CrossMgr\MainWin.py", line 3344, in menuFileHistory
self.openRace( fileName )
File "H:\dev\CrossMgr\Utils.py", line 605, in new_f
return f( *args, **kwargs)
File "H:\dev\CrossMgr\MainWin.py", line 3276, in openRace
if not Utils.MessageOKCancel( self, '{}.\n\n{}:\n\n\t{}{}\n\n{}'.format(
File "H:\dev\CrossMgr\Utils.py", line 326, in MessageOKCancel
return dlg.ShowModal() == wx.ID_OK
File "H:\dev\CrossMgr\env\lib\site-packages\wx\core.py", line 3427, in
lambda event: event.callable(*event.args, **event.kw) )
File "H:\dev\CrossMgr\env\lib\site-packages\wx\lib\mixins\grid.py", line 41, in __DoEnableEdit
self.EnableCellEditControl()
File "H:\dev\CrossMgr\LogPrintStackStderr.py", line 31, in DoLogRecord
traceback.print_stack( file=sys.stderr )
6: ....\src\msw\window.cpp(595): 'SetFocus' failed with error 0x00000057 (The parameter is incorrect.).: <wx._core.LogRecordInfo object at 0x0000024E262D1A20>


  • wxPython Exception: 2025-02-19 18:32:08

File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.3.1.1\plugins\python-ce\helpers\pydev\pydevd.py", line 2270, in
main()
File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.3.1.1\plugins\python-ce\helpers\pydev\pydevd.py", line 2252, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.3.1.1\plugins\python-ce\helpers\pydev\pydevd.py", line 1563, in run
return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.3.1.1\plugins\python-ce\helpers\pydev\pydevd.py", line 1570, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.3.1.1\plugins\python-ce\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "H:\dev\CrossMgr\MainWin.py", line 4564, in
MainLoop()
File "H:\dev\CrossMgr\MainWin.py", line 4561, in MainLoop
app.MainLoop()
File "H:\dev\CrossMgr\env\lib\site-packages\wx\core.py", line 2262, in MainLoop
rv = wx.PyApp.MainLoop(self)
File "H:\dev\CrossMgr\MainWin.py", line 3344, in menuFileHistory
self.openRace( fileName )
File "H:\dev\CrossMgr\Utils.py", line 605, in new_f
return f( *args, **kwargs)
File "H:\dev\CrossMgr\MainWin.py", line 3276, in openRace
if not Utils.MessageOKCancel( self, '{}.\n\n{}:\n\n\t{}{}\n\n{}'.format(
File "H:\dev\CrossMgr\Utils.py", line 326, in MessageOKCancel
return dlg.ShowModal() == wx.ID_OK
File "H:\dev\CrossMgr\env\lib\site-packages\wx\core.py", line 3427, in
lambda event: event.callable(*event.args, **event.kw) )
File "H:\dev\CrossMgr\env\lib\site-packages\wx\lib\mixins\grid.py", line 41, in __DoEnableEdit
self.EnableCellEditControl()
File "H:\dev\CrossMgr\LogPrintStackStderr.py", line 31, in DoLogRecord
traceback.print_stack( file=sys.stderr )
6: ....\src\msw\window.cpp(595): 'SetFocus' failed with error 0x00000057 (The parameter is incorrect.).: <wx._core.LogRecordInfo object at 0x0000024E262D1BD0>


  • wxPython Exception: 2025-02-19 18:32:08

File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.3.1.1\plugins\python-ce\helpers\pydev\pydevd.py", line 2270, in
main()
File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.3.1.1\plugins\python-ce\helpers\pydev\pydevd.py", line 2252, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.3.1.1\plugins\python-ce\helpers\pydev\pydevd.py", line 1563, in run
return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.3.1.1\plugins\python-ce\helpers\pydev\pydevd.py", line 1570, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.3.1.1\plugins\python-ce\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "H:\dev\CrossMgr\MainWin.py", line 4564, in
MainLoop()
File "H:\dev\CrossMgr\MainWin.py", line 4561, in MainLoop
app.MainLoop()
File "H:\dev\CrossMgr\env\lib\site-packages\wx\core.py", line 2262, in MainLoop
rv = wx.PyApp.MainLoop(self)
File "H:\dev\CrossMgr\MainWin.py", line 3344, in menuFileHistory
self.openRace( fileName )
File "H:\dev\CrossMgr\Utils.py", line 605, in new_f
return f( *args, **kwargs)
File "H:\dev\CrossMgr\MainWin.py", line 3276, in openRace
if not Utils.MessageOKCancel( self, '{}.\n\n{}:\n\n\t{}{}\n\n{}'.format(
File "H:\dev\CrossMgr\Utils.py", line 326, in MessageOKCancel
return dlg.ShowModal() == wx.ID_OK
File "H:\dev\CrossMgr\env\lib\site-packages\wx\core.py", line 2346, in Notify
self.notify()
File "H:\dev\CrossMgr\env\lib\site-packages\wx\core.py", line 3552, in Notify
self.result = self.callable(*self.args, **self.kwargs)
File "H:\dev\CrossMgr\LogPrintStackStderr.py", line 31, in DoLogRecord
traceback.print_stack( file=sys.stderr )
6: ....\src\msw\window.cpp(595): 'SetFocus' failed with error 0x00000057 (The parameter is incorrect.).: <wx._core.LogRecordInfo object at 0x0000024E237B4820>
2025-02-19 18:32:13,961 Utils [INFO] openRace: changed FileName to "C:\Users\tim\OneDrive\Documents\2024-12-31-20225 Summer No Frills 2-r2-.cmn".
2025-02-19 18:32:13,980 Utils [INFO] openRace: Excel file "C:\Users\tim\OneDrive\timing-mtb\GMBC\2025-01-31 riders.xlsx"


  • wxPython Exception: 2025-02-19 18:32:14

File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.3.1.1\plugins\python-ce\helpers\pydev\pydevd.py", line 2270, in
main()
File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.3.1.1\plugins\python-ce\helpers\pydev\pydevd.py", line 2252, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.3.1.1\plugins\python-ce\helpers\pydev\pydevd.py", line 1563, in run
return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.3.1.1\plugins\python-ce\helpers\pydev\pydevd.py", line 1570, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.3.1.1\plugins\python-ce\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "H:\dev\CrossMgr\MainWin.py", line 4564, in
MainLoop()
File "H:\dev\CrossMgr\MainWin.py", line 4561, in MainLoop
app.MainLoop()
File "H:\dev\CrossMgr\env\lib\site-packages\wx\core.py", line 2262, in MainLoop
rv = wx.PyApp.MainLoop(self)
File "H:\dev\CrossMgr\env\lib\site-packages\wx\core.py", line 2346, in Notify
self.notify()
File "H:\dev\CrossMgr\env\lib\site-packages\wx\core.py", line 3552, in Notify
self.result = self.callable(*self.args, **self.kwargs)
File "H:\dev\CrossMgr\LogPrintStackStderr.py", line 31, in DoLogRecord
traceback.print_stack( file=sys.stderr )
6: ....\src\msw\window.cpp(595): 'SetFocus' failed with error 0x00000057 (The parameter is incorrect.).: <wx._core.LogRecordInfo object at 0x0000024E26687B50>`

@tjsr
Copy link
Author

tjsr commented Feb 19, 2025

Further - have had it fail in self.EnableCellEditControl() of grid.py:41 from GetClientRect.

  File "H:\dev\CrossMgr\MainWin.py", line 4564, in <module>
    MainLoop()
  File "H:\dev\CrossMgr\MainWin.py", line 4561, in MainLoop
    app.MainLoop()
  File "H:\dev\CrossMgr\env\lib\site-packages\wx\core.py", line 2262, in MainLoop
    rv = wx.PyApp.MainLoop(self)
  File "H:\dev\CrossMgr\MainWin.py", line 3344, in menuFileHistory
    self.openRace( fileName )
  File "H:\dev\CrossMgr\Utils.py", line 605, in new_f
    return f( *args, **kwargs)
  File "H:\dev\CrossMgr\MainWin.py", line 3276, in openRace
    if not Utils.MessageOKCancel( self, '{}.\n\n{}:\n\n\t{}{}\n\n{}'.format(
  File "H:\dev\CrossMgr\Utils.py", line 326, in MessageOKCancel
    return dlg.ShowModal() == wx.ID_OK
  File "H:\dev\CrossMgr\env\lib\site-packages\wx\core.py", line 3427, in <lambda>
    lambda event: event.callable(*event.args, **event.kw) )
  File "H:\dev\CrossMgr\env\lib\site-packages\wx\lib\mixins\grid.py", line 41, in __DoEnableEdit
    self.EnableCellEditControl()
  File "H:\dev\CrossMgr\LogPrintStackStderr.py", line 31, in DoLogRecord
    traceback.print_stack( file=sys.stderr )

Fired from flatnotebook.py:6501.

@esitarski
Copy link
Owner

esitarski commented Feb 19, 2025 via email

@tjsr
Copy link
Author

tjsr commented Feb 20, 2025

It both is an isn't harmless. While it doesn't appear to affect functionality, it means there's an underlying exception that comes from an event which isn't caught, which causes it to track later on that an unhandled exception occurred in an event handler thread so when it goes in to things like exit states, it knows about that so thinks the application or windows aren't being closed cleanly.

It's not my top priority at the moment (much more a case of 'log these things when you encounter them, regardless of plan of action'), but I want to find a way of trapping exceptions that come from certain event handlers so we can at least prevent wxwidgets and the wx.App instance/event handler being in that state.

As far as UI design - you and I need to sit down and have a talk about where you want to go with the project in general. I'd be much happier with a declarative approach to UI, and the current project has very little abstraction. However I haven't yet found from a brief search that there's any good declarative WX libraries, nor anything like a React Native implementation for it.

@tjsr
Copy link
Author

tjsr commented Feb 20, 2025

Alright, I found (and fixed) this issue - it was a little bit in my code but is also present in existing code. When I say 'my code', in my branch I've refactored these classes so that those controls are in their own files and classes, and added a little bit of code to disable the controls when no race is active (ie, when it would just discard anything you enter but you don't know it's done that) - with a message that tells you why it's disabled. But for the most part it's still pretty similar, and affects both.

What happens is that when you call things like

wx.CallLater( 100, self.keypad.numEdit.SetFocus )

wx.CallLater( 100, self.keypad.numEdit.SetFocus )

The state or event handle to these objects can go away in that time before the callback occurs, and it seems it can also happen when you try to set the focus on an object that's marked disabled.

Anyway, it's a mix of things there - so in my reworked version I've wrapped the callback in a safe handler that catches the exception at the time any code is run so you can more easily debug it (same as I've done for all new menu item calls - they live in a wrapped handler that calls a lambda pointing to the method) - but it also has some guard checks in there that prevents it firing if the control is disabled.

I've also changed it so that you now set the focus to the actual pane, and whenever that pane explicitly requests focus, it also sets the editor text field as the taking focus rather than just the background panel. - so any time something programmatically causes those tabs to grab focus, it will be on the text field, ready to accept data entry.

I'll commit this to my fork when I get home this evening and then point you to the branch containing the fixes, and we can figure out what to do with a refactor to bring them together.

Edit: A PR for a branch is here, but it is very much a WIP which needs squashing, cherry-picking and rebasing on main as it's on top of all other work right now. #170
This is definitely very draft state.

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

No branches or pull requests

2 participants