GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-18748: io.IOBase destructor now logs close() errors in dev mode by vstinner · Pull Request #12786 · python/cpython

Conversation

@vstinner

@vstinner vstinner commented Apr 11, 2019 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

The development mode (-X dev) now logs exceptions in io.IOBase
destructor if close() fails. These exceptions are silent by default.

https://bugs.python.org/issue18748

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Apr 11, 2019
@bedevere-bot bedevere-bot added the awaiting core review label Apr 11, 2019
@vstinner
vstinner requested review from methane, pablogsal, pitrou and vadmium April 11, 2019 14:13
@vstinner

vstinner commented Apr 11, 2019

Copy link
Copy Markdown
Member Author

That's a shy fix for https://bugs.python.org/issue18748#msg339979 : only log I/O error on close() in development mode (-X dev). Maybe we should even stop to silence these errors by default?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
vstinner requested a review from serhiy-storchaka April 11, 2019 14:15
serhiy-storchaka
serhiy-storchaka reviewed Apr 11, 2019
View reviewed changes
Comment thread Modules/_io/iobase.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@pitrou

pitrou commented Apr 11, 2019

Copy link
Copy Markdown
Member

Have you tried running e.g. the test suite with -Xdev and see how many of those errors pop up?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Apr 11, 2019

Copy link
Copy Markdown
Member Author

Example with bug.py from https://bugs.python.org/issue18748#msg339979.

Without the change.

$ ./python bug.py 
libgcc_s.so.1 must be installed for pthread_cancel to work
Aborted (core dumped)

... Python silently ignores the EBADF error on close(3) causing weird bug which calls abort() and creates a coredump :-( Or sometimes, you are lucky and nothing happens. Example with -X dev (but without my change):

$ ./python -X dev bug.py 
bug.py:28: ResourceWarning: unclosed file <_io.TextIOWrapper name='a.txt' mode='w+' encoding='UTF-8'>
  main()
ResourceWarning: Enable tracemalloc to get the object allocation traceback

ResourceWarning isn't the real bug here, it's more ore less fine to rely on Python to automatically close files usually.

With the change.

$ ./python -X dev bug.py 
bug.py:28: ResourceWarning: unclosed file <_io.TextIOWrapper name='a.txt' mode='w+' encoding='UTF-8'>
  main()
ResourceWarning: Enable tracemalloc to get the object allocation traceback
Exception ignored in: <_io.TextIOWrapper name='a.txt' mode='w+' encoding='UTF-8'>
OSError: [Errno 9] Bad file descriptor

The interesting part is the new exception which contains the filename a.txt, useful to identify the bug, and the exception: OSError: [Errno 9] Bad file descriptor.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Apr 11, 2019

Copy link
Copy Markdown
Member Author

Have you tried running e.g. the test suite with -Xdev and see how many of those errors pop up?

Good idea. Not yet, will do.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Apr 11, 2019

Copy link
Copy Markdown
Member Author

So what is your preference? Continue to silence I/O errors on close() by default (when called from the destructor)? Or always log these errors?

In my experience, EBADF can cause a lot damage and are hard to detect when Python makes them slient.

But here, the code contains the comment:

        /* Silencing I/O errors is bad, but printing spurious tracebacks is
           equally as bad, and potentially more frequent (because of
           shutdown issues). */

So I proposed the compromise of reusing -X dev...

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pitrou

pitrou commented Apr 11, 2019

Copy link
Copy Markdown
Member

I think these should be silenced by default.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Apr 11, 2019

Copy link
Copy Markdown
Member

I think there is a bug in the io streams code that causes EBADF. Logging it in debug mode will help us to fix it.

Perhaps there are other places where an error is silenced intentionally because otherwise it would confuse users by spurious tracebacks. It would worth to enabling logging in debug mode in all such cases.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka
serhiy-storchaka approved these changes Apr 11, 2019
View reviewed changes
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting core review labels Apr 11, 2019
@vstinner

vstinner commented Apr 11, 2019

Copy link
Copy Markdown
Member Author

It would worth to enabling logging in debug mode in all such cases.

Do you mean to enable these logs by default in debug build, ./configure --with-pydebug and "#ifdef Py_DEBUG"? I agree.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Apr 11, 2019

Copy link
Copy Markdown
Member Author

Have you tried running e.g. the test suite with -Xdev and see how many of those errors pop up?

Hopefully, only test_io and test_urllib log such errors. In fact that most of Python code base is sane :-)

I pushed a fix to silence most destructor errors in test_io, but not all of them. I didn't touch test_urllib.

Some tests of CBufferedRWPairTest log "Exception ignored in: ..." but I'm not sure that it's a bug in the test or in io.BufferedRWPair. buffered_close() which doesn't call parent IOBase.close() method, wheareas _io_FileIO_close_impl() does call it. I'm always lost in the code base of the io module, so I can be wrong ;-)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Apr 11, 2019

Copy link
Copy Markdown
Member Author

I'm not sure how to fix test_urllib yet. Maybe it should be fixed in a separated change.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

methane
methane approved these changes Apr 12, 2019
View reviewed changes
@vstinner
bpo-18748: io.IOBase destructor logs close() exceptions
b029db6
In development mode (-X dev) and in debug build, the io.IOBase
destructor now logs close() exceptions. These exceptions are silent
by default in release mode.
@vstinner

vstinner commented Apr 12, 2019

Copy link
Copy Markdown
Member Author

Hum, fixing tests is non-trivial. Rather than pushing half baken fix for test_io and no fix for test_urllib, I modified my PR to make the minimum change: start to log exceptions. Instead, I will create a separated PR to fix test_io and another to fix test_urllib.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
Fix test_io in debug build
e7647da
@vstinner

vstinner commented Apr 12, 2019

Copy link
Copy Markdown
Member Author

Oh, I reverted too many fixes from test_io. This PR needs a minimum fix for test_io to not break the CI :-)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Apr 12, 2019

Copy link
Copy Markdown
Member Author

Have you tried running e.g. the test suite with -Xdev and see how many of those errors pop up?

I also checked our buildbot configuration: such "ignore exception" is logged in the "warnings" of a buildbot, and a build becomes orange in that case.

https://github.com/python/buildmaster-config/blob/fd08bbf525d55b10c2eba7916534eb20e06a4232/master/custom/steps.py#L25-L27

        # PyErr_WriteUnraisable() exception: usually, error in
        # garbage collector or destructor
        r"Exception ignored in:",
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
vstinner merged commit 4423504 into python:master Apr 12, 2019
@bedevere-bot bedevere-bot removed the awaiting merge label Apr 12, 2019
@vstinner
vstinner deleted the log_io_err_devmode branch April 12, 2019 15:06
@vstinner

vstinner commented Apr 12, 2019

Copy link
Copy Markdown
Member Author

Maybe the fact that close() exceptions are ignored silently in io.IOBase constructor should be better documented, but I'm not sure where it should be documented. If someone has an idea, please go ahead and write a pull request :-)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ionelmc ionelmc mannequin mentioned this pull request Apr 10, 2022
io.IOBase destructor silence I/O error on close() by default #62948
Closed
@cmaloney cmaloney mentioned this pull request Feb 26, 2025
gh-129726: Break gzip.GzipFile reference loop #130055
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@methane methane methane approved these changes
@serhiy-storchaka serhiy-storchaka serhiy-storchaka approved these changes
@pitrou pitrou Awaiting requested review from pitrou
@pablogsal pablogsal Awaiting requested review from pablogsal
@vadmium vadmium Awaiting requested review from vadmium

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@vstinner @pitrou @serhiy-storchaka @methane @the-knights-who-say-ni @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.