-
Notifications
You must be signed in to change notification settings - Fork 253
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
WorkerThread.Dispose might access an already released FWorkEvent #1012
Comments
Why that? Have you seen an actual problem with that code? |
No, I most definitely did not, so you're of course free to not consider this a bug. But your argumentation sort of demonstrates my point: the current code makes assumptions about when it is called and what state the thread is in at that moment. This can be avoided with the code I suggested, so it would be more robust about possible changes in the future that might invalidate any of those assumptions. I actually came across this while trying to figure out what is causing the exceptions I see in the debugger with the new unit tests I wrote for #1001. Since these always relate to |
I think this is quite normal and OK as long as this happens within a class like here.
Which tool did you use?
Any chance you send a pull request so that I can take a look? |
Sure there's nothing inherently wrong here, but the less assumptions the code needs to make, the more resilient it is to possible future changes.
Of course it assumes that the thread checks I also don't see how setting the event in that way could cause a race-condition - it pretty much does the same thing as the current code, but without the assumption that the thread is waiting by the time An overridden
I didn't use a tool (and I'm not aware of one for Delphi code that is able to detect these sort of things) - in other words, it was just a fancy word for "looking at the code", but with a different mindset (what could possibly go wrong) and looking for familiar and unfamiliar code patterns.
You already have the test code in https://github.com/JAM-Software/Virtual-TreeView/blob/master/Tests/VTWorkerThreadIssue1001Tests.pas. If I run this in the IDE with Win32, I see exceptions on the |
I tried 500 iterations and 100k nodes, Win32 and Win64, but didn't see any exception. I used the latest code with some changes of yesterday evening. |
Okay thanks for letting me know - I was afraid that could happen, because the exceptions seem very odd in that they only happen when debugging and only on Win32. I also spent way too much time trying to figure out what's going on there, so it will have to remain a mystery for now, I'm afraid. |
Doesn't it correspond to #1245 ? |
I think you are right. Since #1245 is closed, this may explain why I was unable to reproduce the failure. Anyway, I am closing this issue now, I'm quite sure that there is no problem left. We are using VTV heavily in our products at JAM Software, I think we would have noticed that. |
If
TWorkerThread.Dispose
is called withCanBlock = False
(the way it is currently used), it could access an already releasedFWorkEvent
:Because of
FreeOnTerminate = True
, as soon asTerminate()
has been called,WorkerThread
can be disposed at any given time, so it is unsafe to access any of its fields or methods.I realize that most of the times, this might not be a problem, because
WorkerThread
is stuck inWaitForSingleObject
- but there's no guarantee for that.To fix this,
FWorkEvent
should be set in an overriddenTerminatedSet
:With that, Dispose could be simplified to:
The text was updated successfully, but these errors were encountered: