-
Notifications
You must be signed in to change notification settings - Fork 724
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
Unsafe to use TestContext.CurrentContext.TestDirectory #2872
Comments
I am closing old uncategorized issues that have not had comments or made progress in several years. If anyone comes back with a compelling argument for these issues, we can reopen. |
@rprouse, I understand you'd want to clean up, but if this is still a bug, it's a nasty one when you hit it. I just checked the code of the project that ran into this, which is still in development, and I noticed comments that explicitly say "don't use The way I see it: currently, combining
My guess is that, when people see All that said, I haven't seriously tried the old-style approach for a while and maybe this bug was resolved meanwhile by other commits? |
On a more general note: static methods, properties etc are best coded very defensively and thread-safe. |
@abelbraaksma can you produce a simple test case that we can use to reproduce and fix this issue? I am reopening with the confirm label. |
@rprouse @abelbraaksma I think the central issue here is that there is (or was) no I said "was" above because, at a certain point, we allowed ourselves to be convinced that creating a sort of artificial Obviously, you can't do that now as too much time has passed but this may be something to put on the list or NUnit 4.0. WRT socket exceptions, I can't see how that would happen unless the loaded test assembly is accessed through a remote connection, but I don't think we ever supported that. |
No, everything was local. My guess is that it was caused in the RPC communication layer between the main process and the test process, and perhaps some race condition (the stack trace shows a lot of I still maintain the code that caused this at the time. I'll try to find out whether it still reproduces with recent versions of the nunit gui. |
@abelbraaksma AFAICS we haven't been talking about a GUI before this in the thread. Did I miss something? |
@CharliePoole, My bad, it was this one. I actually don't even know if that spawns extra processes. But I'll test with the gui as well just in case. |
Actually there is no longer an NUnit project GUI - there was one in V2. I maintain a separate GUI in a project of my own - the In the case of nunint3-console, a separate process is spawned by default. Most likely something in your test is crashing that process and you are seeing the resulting remoting exception and not the original cause. If your test assembly will run in process, you may get more information. Try using the |
I've checked my commit history related to this thread (actually, parts referred to nunit/nunit-console#405, which is not available anymore, others linked to the current thread #2872). The commit-comments further link to #1834, which describes exactly the same This, in turn, is then fixed in nunit/nunit-console#223. Looking back at those commit messages I can distill first a rollback from NUnit 3.10 to 3.6, and a few commits later it appeared that some Next commits upgrade everything to 3.10 and remove any version inconsistencies throughout the projects and solutions. Nonetheless, the comment in the code remained, using an alternative way for getting Today I ran all tests with this workaround disabled, and there were no crashes related to NUnit, no Bottom line: the Apologies for the request for reopening, the bug was truly recognized and resolved (prior to my own reporting).
Good to know this if I need to investigate any future issue I might have. Thanks! |
Glad we're all sorted! 😄 |
When using
TestContext.CurrentContext.TestDirectory
several things can happen:CurrentContext
is nullSocketException
is thrownThe first two can be worked around, but the last one cannot be caught in a try/catch when calling
TestDirectory
. I'm not sure why, but my suspicion is that the error is thrown asynchronously.I think that generally speaking, property gettors should either return null (if there's reason for such a case) or return a legal value, but they should not raise exceptions. Even more so when they are static, as this leads to hard-to-debug exceptions in static cctors.
This behavior is most apparent when running tests with
nunit3-console.exe
, where tests are loaded throughTestSourceAttribute
which require access to resources relative to the local directory. In such cases it makes sense to useTestContext.CurrentContext.TestDirectory
, as other methods are unreliable, but unfortunately, the preferred method is unsafe and may crash your whole test-session.Extra info:
The stacktrace of the
SocketException
, which is often thrown, but not always in the mentioned scenarios, is as follows:The text was updated successfully, but these errors were encountered: