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
BUG: Clean up xout before rethrow #31
BUG: Clean up xout before rethrow #31
Conversation
Elastix did not call AfterRegistrationBase before throwing exception and consequently did not clean up xout. This resulted in undefined behaviour and more or less random segfaults if the ElastixFilter was called again after an exception was thrown. This would happen _even_ if the filter was completely destroyed and a new filter was instantiated. This possibly fixes SuperElastix/SimpleElastix#123 and an issue previously reported by @coertmetz but need more testing. NOTE: This does not change the fact that elastix is not thread safe which might still be an issue (also reported by @coertmetz).
This seems to make a lot of sense. I do not have enough knowledge about AfterRegistrationBase to get an idea of possible side-effects however. |
so AfterRegistrationBase() currently contains one line: xl::xout.RemoveTargetCell( "iteration" ); I am not sure why this cell should be removed before throwing, but your explanation makes intuitive sense to me. At the least I see no harm in removing it, as this particular exception completely stops the registration altogether and (safely) terminates elastix. @kaspermarstal Did you do a test with and without this change and indeed observed segfaults going away? |
Yes, @woutdenolf made a minimum working example with data in SuperElastix/SimpleElastix#123. You can get it there @coertmetz. Yes, this patch fixes the problem on my machine -- I have not observed any segfault with the patch applied. However I have not been able to find out exactly what is going wrong when AfterRegistrationBase is not called. Sometimes it segfaults, sometimes it doesn't. I can only assume that the logger somehow ends up in an inconsistent state if "iteration" is not removed -- but why or how I do not know. |
Ok, so if it also works at Coert's side, then please go ahead with merging. In case Coert doesn't have time, go ahead with merging also. |
Is it something you plan to test @coertmetz? |
The iteration info output is of type xoutrow. And this is the only occasion where xoutrow is used. So maybe the underlying bug is in that class (could be, as it is the most complicated xout type). Maybe the destructor of xoutrow does not properly clean up itself for example. |
Hi @stefanklein, This is more or less what we now do at Quantib side as a work around. We catch the exceptions of elastix. In the catch block we do:
For this to work we added the xout_valid to the xoutlibrary. This method just checks if local_xout is not null:
But of course, this should in principle be fixed within elastix. |
…suggestd by @coertmetz, and do it directly, instead of AfterRegistrationBase as suggested by @stefanklein
to get these changes, would I need to rebuild the project, or is it sufficient to just pull down the updated repo? |
Elastix did not call AfterRegistrationBase before throwing exception and consequently did not clean up xout. This resulted in undefined behaviour and more or less random segfaults if the ElastixFilter was called again after an exception was thrown. This would happen even if the filter was completely destroyed and a new filter was instantiated because xout is a global static object. This possibly fixes SuperElastix/SimpleElastix#123 and an issue previously reported by @coertmetz.
The PR needs more testing and thorough review before merge. In addition, we should review exception handling elsewhere - e.g. for transformix.
NOTE: This does not change the fact that elastix is not thread safe which might still be an issue (also reported by @coertmetz). The logger will most likely have to be completely refactored into a member variable on e.g. ElastixMain for the library interface to become thread safe.