Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Change Debug.Assert on Windows when no debugger attached - #4212

Merged
stephentoub merged 1 commit into
dotnet:masterfrom
stephentoub:debug_write_stderr
Oct 28, 2015
Merged

stephentoub merged 1 commit into
dotnet:masterfrom
stephentoub:debug_write_stderr

Conversation

@stephentoub

Copy link
Copy Markdown
Member

cc: @ellismg, @mmitche, @davidsh, @weshaggard
What do you guys think of this behavior change to what we have with Assert today on Windows?

Today on Windows if no debugger is attached, Debug.Assert fail fasts, which makes diagnosing difficult in a continuous integration environment; it also causes any other tests in the same process to be abandoned.

This commit changes Assert to instead write to stderr and throw an exception in such situations. The exception helps to ensure that code containing the assert is alerted to the failure, and the stderr output helps to ensure that it's noticed even if the exception is eaten.

Today on Windows if no debugger is attached, Debug.Assert fail fasts, which makes diagnosing difficult in a continuous integration environment; it also causes any other tests in the same process to be abandoned.

This commit changes Assert to instead write to stderr and throw an exception in such situations.  The exception helps to ensure that code containing the assert is alerted to the failure, and the stderr output helps to ensure that it's noticed even if the exception is eaten.
@davidsh

davidsh commented Oct 28, 2015

Copy link
Copy Markdown
Contributor

This commit changes Assert to instead write to stderr and throw an exception in such situations. The exception helps to ensure that code containing the assert is alerted to the failure, and the stderr output helps to ensure that it's noticed even if the exception is eaten.

This will be a big improvement from what we have now.

@mellinoe

Copy link
Copy Markdown
Contributor

This LGTM. I did the refactoring of this a long while ago, when we were splitting and consolidating code between .NET Native and CoreCLR. Previously the code here (on CoreCLR) was popping up the message box window, which we decided to remove as it didn't seem appropriate and definitely couldn't be shared with .NET Native. But the current behavior definitely isn't very good, especially when no debugger is attached, so this is definitely far better.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the behavior of this if I have non ASCII characters in the string? Does the default fallback just put in '?'

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the default fallback just put in '?'

Yup.

@ellismg

ellismg commented Oct 28, 2015

Copy link
Copy Markdown
Contributor

LGTM. Thanks for this improvement!

@stephentoub

Copy link
Copy Markdown
Member Author

Thanks for the reviews, Matt, Eric, and David.

stephentoub added a commit that referenced this pull request Oct 28, 2015
Change Debug.Assert on Windows when no debugger attached
@stephentoub
stephentoub merged commit 09a66bf into dotnet:master Oct 28, 2015
@stephentoub
stephentoub deleted the debug_write_stderr branch October 28, 2015 23:23
stephentoub added a commit to stephentoub/corefx that referenced this pull request Oct 29, 2015
This is another attempt at dotnet#4212, which was then immediately reverted in dotnet#4220 because it broke some internal build stuff due to the APIs being used.

Changes on Windows:
- When an Assert fires and the debugger isn't attached, instead of Environment.FailFast'ing, we throw an exception.
- When an Assert fires, we were previously calling Debug.WriteLine twice; now we only call it once.

Changes on Unix:
- Made the Assert functionality match that of Windows in terms of the APIs being called.
- Changed WriteCore to check Debugger.IsLogging and, if it's true, do a Debugger.Log, just as we do on Windows.  Right now these are nops, but when that changes this will become useful.  The fallback when IsLogging is false remains the same: write to syslog.
- Fixed the WriteToStderr function to ignore errors from Write, as we don't want this function throwing exceptions due to failed attempts to output to stderr, such as if it was piped to another program that ended before this one (as with dotnet#4221).
@karelz karelz modified the milestone: 1.0.0-rtm Dec 3, 2016
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…derr

Change Debug.Assert on Windows when no debugger attached

Commit migrated from dotnet/corefx@09a66bf
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
This is another attempt at dotnet/corefx#4212, which was then immediately reverted in dotnet/corefx#4220 because it broke some internal build stuff due to the APIs being used.

Changes on Windows:
- When an Assert fires and the debugger isn't attached, instead of Environment.FailFast'ing, we throw an exception.
- When an Assert fires, we were previously calling Debug.WriteLine twice; now we only call it once.

Changes on Unix:
- Made the Assert functionality match that of Windows in terms of the APIs being called.
- Changed WriteCore to check Debugger.IsLogging and, if it's true, do a Debugger.Log, just as we do on Windows.  Right now these are nops, but when that changes this will become useful.  The fallback when IsLogging is false remains the same: write to syslog.
- Fixed the WriteToStderr function to ignore errors from Write, as we don't want this function throwing exceptions due to failed attempts to output to stderr, such as if it was piped to another program that ended before this one (as with dotnet/corefx#4221).


Commit migrated from dotnet/corefx@68e46ff
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants