You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
run crashes on Linux if run outside of main thread #70
This is an issue because RasCAL-2 needs long-running code (e.g. RAT.run()) to run outside the main loop, as the main loop is used for handling and updating the GUI itself. It seems to run successfully on Windows (@DrPaulSharp managed to run it okay). If anyone else would like to run the script on whatever devices they have to hand that'd be useful!
Various observations:
This happens for any of the standard_layers or custom_XY examples but NOT the custom_layers examples which run successfully.
On DSPC_standard_layers it happens no matter what procedure is chosen.
From where it crashes, it seems that we enter the C++ code before it breaks; if using a non-calculate procedure, the Running [procedure] message is produced before it segfaults.
Thanks Alex, I can confirm that I can run the test ok on windows, but I get a seg fault on linux (specifically through using the Windows Subsystem for Linux).
Irrespective of this issue, I will recommend using a Process instead of a python thread as the latter will affect the responsiveness of the GUI. The main problem with the python process is that it uses pickling to copy inputs i.e. Project and Controls but because of some issue the Project class cannot be pickled (Apologies, for some reason we did not open an issue on GitHub).
We could either try to fix the pickling issue so we can use the run function
@StephenNneji in the GUI code i was using a QtCore.QThread, which seems to be the recommended way to keep long-running things 'out of the way' without affecting the GUI. the issue above uses a regular Python thread in order to be a smaller reproduceable example (and to confirm that the segfault is on our end and not Qt's!)
Memory checking shows an Invalid write of size 8 in the following code from allocateLayersForContrast.cpp:
n = coder::internal::intlength(contrastLayers.size(0), contrastLayers.size
(1));
thisContrastLayers_size[0] = n;
thisContrastLayers_size[1] = 5;
for (i = 0; i < 5; i++) {
for (i1 = 0; i1 < n; i1++) {
thisContrastLayers_data[i1 + n * i] = 0.0;
}
}
Specifically, the line thisContrastLayers_data[i1 + n * i] = 0.0;. This line is merely the initialisation of the array, so does not read in any RAT data. As far as I can tell, the array indices are correct, and the array is of type double, so I'm not sure what the cause of the invalid memory access is.
We also find a memory issue in SLDFunction.cpp. In this code, we construct arrays for all values less than and greater than a particular function. It seems to me that there is a possibility that in the way this is done in the C++ code there is the possibility of an empty array, but this doesn't occur in the matlab code.
doesn't segfault. even stranger is that if you import it outside the thread and then again inside it does segfault, but i may just be missing some detail of python import implementation here:
I've had a fresh look at allocateLayersForContrast.m in the MATLAB source:
@arwelHughes Can you please confirm whether the code on line 35 should instead read: thisContrastLayers(i, :) = []; instead of thisContrastLayers(1, :) = [];. I can't guarantee this bug is causing the memory issues, but we should look to fix it urgently regardless.
Hi Paul,
Well, firstly I have a different version to you – did you put the ‘disp’ in for debugging?
***@***.***
It probably should be ‘thisContrastLayers(I,:)…. as you suggest, but in practice it makes no difference. That line is never actually reached in normal usage (because there will never be an empty layer), but I think it was necessary because otherwise Coder throws a ‘thisContrastLayers is not fully defined on all execution paths’ error (I think!).
Cheers,
Arwel
From: Paul Sharp ***@***.***>
Date: Monday, 2 September 2024 at 17:31
To: RascalSoftware/python-RAT ***@***.***>
Cc: Hughes, Arwel (STFC,RAL,ISIS) ***@***.***>, Mention ***@***.***>
Subject: Re: [RascalSoftware/python-RAT] run crashes on Linux if run outside of main thread (Issue #70)
I've had a fresh look at allocateLayersForContrast.m in the MATLAB source:
image.png (view on web)<https://github.com/user-attachments/assets/31d9b618-85a5-4d22-8caf-c6688f8b3dee>
@arwelHughes<https://github.com/arwelHughes> Can you please confirm whether the code on line 35 should instead read: thisContrastLayers(i, :) = [];. I can't guarantee this bug is causing the memory issues, but we should look to fix it urgently regardless.
—
Reply to this email directly, view it on GitHub<#70 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AGMNVXUMIHUG5SAJXSNJDM3ZUSHEZAVCNFSM6AAAAABM4FBUK2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMRVGA3TIOJZHE>.
You are receiving this because you were mentioned.Message ID: ***@***.***>
since we've merged #74 i've found that RATMain does not produce a segfault in a separate process (only a separate thread). so this issue is a lot less urgent since we can use processes for multiprocessing in the GUI!
Activity
DrPaulSharp commentedon Aug 21, 2024
Thanks Alex, I can confirm that I can run the test ok on windows, but I get a seg fault on linux (specifically through using the Windows Subsystem for Linux).
alexhroom commentedon Aug 22, 2024
some debugging: running in
gdb python3:for custom XY:
for standard layers:
alexhroom commentedon Aug 22, 2024
it seems that there are various issues around the GIL when it comes to calling Python from inside C threads?
python/cpython#111034
https://docs.python.org/3/c-api/init.html#non-python-created-threads
unsure if the problem may be that
StephenNneji commentedon Aug 22, 2024
Irrespective of this issue, I will recommend using a Process instead of a python thread as the latter will affect the responsiveness of the GUI. The main problem with the python process is that it uses pickling to copy inputs i.e. Project and Controls but because of some issue the Project class cannot be pickled (Apologies, for some reason we did not open an issue on GitHub).
We could either try to fix the pickling issue so we can use the
runfunctionRAT.run(project, control)or we could just pickle the inputs to RATMain
RATapi.rat_core.RATMain(problem_definition, cells, limits, cpp_controls, priors )alexhroom commentedon Aug 22, 2024
@StephenNneji in the GUI code i was using a
QtCore.QThread, which seems to be the recommended way to keep long-running things 'out of the way' without affecting the GUI. the issue above uses a regular Python thread in order to be a smaller reproduceable example (and to confirm that the segfault is on our end and not Qt's!)StephenNneji commentedon Aug 22, 2024
As far as I know, QThread are just a wrapper on the python thread https://stackoverflow.com/questions/1595649/threading-in-a-pyqt-application-use-qt-threads-or-python-threads except if something has been changed in PyQt6
alexhroom commentedon Aug 22, 2024
yeah, with the main benefits of the wrapper being that it handles signals/slots and so on to avoid the thread choking up the GUI! as in the post:
will try it with a ProcessPoolExecutor when we can get the thing pickled correctly anyway and see if it's more performant
DrPaulSharp commentedon Aug 22, 2024
Memory checking shows an
Invalid write of size 8in the following code fromallocateLayersForContrast.cpp:Specifically, the line
thisContrastLayers_data[i1 + n * i] = 0.0;. This line is merely the initialisation of the array, so does not read in any RAT data. As far as I can tell, the array indices are correct, and the array is of type double, so I'm not sure what the cause of the invalid memory access is.DrPaulSharp commentedon Aug 22, 2024
We also find a memory issue in
SLDFunction.cpp. In this code, we construct arrays for all values less than and greater than a particular function. It seems to me that there is a possibility that in the way this is done in the C++ code there is the possibility of an empty array, but this doesn't occur in the matlab code.alexhroom commentedon Sep 2, 2024
fascinatingly, it works completely fine if RAT is imported inside the thread:
doesn't segfault. even stranger is that if you import it outside the thread and then again inside it does segfault, but i may just be missing some detail of python import implementation here:
DrPaulSharp commentedon Sep 2, 2024
I've had a fresh look at
allocateLayersForContrast.min the MATLAB source:@arwelHughes Can you please confirm whether the code on line 35 should instead read:
thisContrastLayers(i, :) = [];instead ofthisContrastLayers(1, :) = [];. I can't guarantee this bug is causing the memory issues, but we should look to fix it urgently regardless.arwelHughes commentedon Sep 3, 2024
DrPaulSharp commentedon Sep 3, 2024
Thanks Arwel. Yes, the disp lines are for debugging.
alexhroom commentedon Sep 4, 2024
since we've merged #74 i've found that RATMain does not produce a segfault in a separate process (only a separate thread). so this issue is a lot less urgent since we can use processes for multiprocessing in the GUI!
DrPaulSharp commentedon Sep 12, 2024
closed by #78