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
Python on Windows disables all C runtime library assertions #49054
Comments
This block in exceptions.c: #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
...
/* turn off assertions in debug mode */
prevCrtReportMode = _CrtSetReportMode(_CRT_ASSERT, 0);
#endif Does exactly what the comment says it does - disables all assertions. The change was introduced in: Fix the CRT argument error handling for VisualStudio .NET 2005. Install which seems like a very large hammer to be using (ie, the problem |
But then you get a nasty pop-up window on code like this:
Instead of a gentle "OSError: [Errno 9] Bad file descriptor" |
Yes, this is the biggest reason why this is required. Kristján |
Btw, So, as I said, we could very well avoid this by just wrapping a few instances of close() with a CrtSetErrorHandler calls and friends. With the caveat that these are not threadsafe, that is, it is a process global setting for the CRT. But toggling it during a limited scope surely is less intrusive than modifying it for the entire process permanently. Kristján |
It would be interesting to know which tests actually fail. If the tests |
I guess another option is to expose it via msvcrt and let the test |
Ok, I'll prepare a patch to the trunk. I've had some experience with this and should have the code lying around somewhere. I'll let you know once I´ve created an issue. |
As long as the CRT contains bogus assertions (that violate standard C), |
We've had this discussion before, you know, 2006. K |
I remember, and you were already wrong back then :-) MS asserts (in winsig.c) that the signal number in signal() is one It is true that Python *also* invokes undefined behavior in certain |
Can anyone point me at a test that failed in this case? A quick look On the broader point, it could be argued that if it is the apps |
See bpo-1350409 (and subsequently bpo-1167262 and bpo-1311784). Python If you were looking for a test case that fails specifically - there
Ideally, assertions should be enabled in debug builds, and disabled (it's not just that the signal issue is a standards violation. Assuming
It's the other way 'round: it rejects all but a list of known signals. |
I submit to you a patch to the trunk which narrows down the problem. In Yesterday I put in some tests in to the testsuite to excercise this |
-1. Setting the CRT report mode is not thread-safe, so this code may |
Martin, |
Adding stuff into _msvcrt is fine with me. I guess the debate then might |
It is not an issue with debug builds only. The crt error handler is The current way we do thing is also not thread safe WRT other IMHO, the issue for the posix fd functions isn't that great, since I'll put in some more effort then, because I think it is really |
Okay, here is a second patch. |
Kristjan, please understanding that setting the CRT error handling is
As a net result, the original error handling is *not* restored. |
I understand thread-safe. This usage is safe from Python threads It is, however, unsafe if some other random thread is also modifying In your example, T2 doesn't hold the GIL and so, this is the scenario This is however not likely to be the case because these settings are Furthermore, since your argument assumes a rogue thread modifying the I think that the drawbacks of modifying the CRT behaviour unexpectedly Now, I can see a compromise we could make. I could make this a compile |
Ooops, ignore my last comment. Hmm, back to the drawing board, I suppose. |
Here is what I think is a better attempt at the selective-disabling. I |
No, it is not. See below.
Assume that T2 is a Python thread. First, it doesn't hold the GIL.
No, it does not. Regular Python threads can break under this patch. |
(sorry, too late - I only read this after responding to the earlier one) |
I created bug 5116 with a patch to expose CrtSetDebugMode via the msvcrt |
I think we ought to make sure that python doesn't crash, even if one Martin has yet to comment on it. Although he was right that a previous |
I believe your new patch suffers the same problem. Consider the blocks + /* turn off crt asserts on windows since we have no control over fd */ And consider Martin's comments in msg80191: Here you have T1 setting a |
Ah, but I think you missed the main point of the new patch. This is similar in some ways to the special version of |
Sorry for interruption. Maybe is _CrtSetReportHook useful?
This is just impression from MSDN document, I didn't try yet. |
Of course, this won't work someone else altered hook function. |
Interesting idea, but perhaps even more convoluted than my (admittedly) fyi, I've added this issue with MS: |
But it doesn't actually crash does it? It throws an assertion dialog,
I think it is enough. The tests are explicitly checking insane input to Hirokazu Yamamoto's suggestion sounds reasonable too, but still doesn't Martin has the final say on what actually gets accepted though :) |
Well, as long as you think that an assertion is fine for the fd |
Maybe already conclusion is met, so probably I shouldn't post this patch I'm not sure which way is the best, but I'm happy if popup dialog on |
Just impression. I feel Martin's "disable globally, and provide API to |
Right. So with the current implementation, they notice immediately that
Except that Python messes with it, so in the small window where the |
I think that is easy to answer: Python should validate all parameters, |
Ok. I have submitted methods to verify the input to strftime() and |
How about _get_osfhandle()? I cannot see _ASSERTE in that function on |
On VS8.0, _get_osfhandle() begins with three assert statements. |
fopen.patch is fine, please apply. |
As for checking CRT handles: it seems that __pioinfo is exported from |
FYI, Microsoft has responded to my query: In short: Thanks for the report. We will probably not be able to address this in Pat Brenner |
Checked in r69268, to check strftime() and fopen() |
Here is the patch implementing msg81093. (check_fd.patch) |
I've taken the patch from Hirokazu and enhanced it:
A VS2008 compilation runs the testsuite just fine. Maybe Hirozaku can |
Sorry, I don't have VS2005. By the way, _PyVerify_fd seems to be required to VC6 too. Because |
I see. I had thought your code was for VS2005 (VC8) |
I agree. Please focus on _MSC_VER >= 1400. I'll post new issue about VC6 |
I think the comment (an invalid fd would be a C program bug) [...] Otherwise, -1 shall be returned and errno set to indicate the error. This is consistent throughout the entire API: the behavior for invalid There should be a global declaration of _Py_verify_fd. I would also |
Ok, how about this comment, Martin? /* Microsoft CRT in VS2005 and higher will verify that a filehandle is
Also, I've added the following to fileobject.h, not coming up with a #if defined _MSC_VER && _MSC_VER >= 1400
/* A routine to check if a file descriptor is valid on Windows.
Returns 0
* and sets errno to EBADF if it isn't. This is to avoid Assertions
* from various functions in the Windows CRT beginning with
* Visual Studio 2005
*/
int _PyVerify_fd(int fd);
#else
#define _PyVerify_fd(A) (1) /* dummy */
#endif |
The revised patch looks fine to me, please apply. |
Committed r69495 |
I opened new issue related to VC6 as bpo-5204. |
Why is this issue still open? |
Closed as stated committed r69495. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: