Skip to content

bpo-29564: warnings suggests to enable tracemalloc #10486

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

Merged
merged 2 commits into from
Nov 13, 2018
Merged

bpo-29564: warnings suggests to enable tracemalloc #10486

merged 2 commits into from
Nov 13, 2018

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Nov 12, 2018

The warnings module now suggests to enable tracemalloc if the source
is specified, tracemalloc module is available, but tracemalloc is not
tracing memory allocations.

https://bugs.python.org/issue29564

The warnings module now suggests to enable tracemalloc if the source
is specified, tracemalloc module is available, but tracemalloc is not
tracing memory allocations.
@@ -77,6 +83,9 @@ def _formatwarnmsg_impl(msg):
if line:
line = line.strip()
s += ' %s\n' % line
elif not tracing:
s += (f'{category}: Enable tracemalloc to get the object '
f'allocation traceback\n')
Copy link
Member

@pablogsal pablogsal Nov 12, 2018

Choose a reason for hiding this comment

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

Question: Would be useful to add to this sentence how to enable tracemalloc quickly? For example:

s += (f'{category}: Enable tracemalloc to get the object '
      f'allocation traceback (i.e. -X tracemalloc=N_FRAMES)\n')

Copy link
Member Author

Choose a reason for hiding this comment

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

There are multiple ways to enable tracemalloc: PYTHONTRACEMALLOC=nframe and python3 -X tracemalloc=nframe. It's non-obvious where -X tracemalloc comes from, nor what is nframe, etc.

"ResourceWarning: Enable tracemalloc to get the object allocation traceback" is already 74 characters long, and I like to fit into 80 columns :-)

IMHO giving the hint "enable tracemalloc" should be enough to someone motivated to fix a ResourceWarning bug :)

@pablogsal: What do you think? Is the current PR good now?

Copy link
Member

@pablogsal pablogsal Nov 13, 2018

Choose a reason for hiding this comment

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

Yup :)

I am convinced by your 80 columns argument :D

Lib/warnings.py Outdated
except Exception:
# When a warning is logged during Python shutdown, tracemalloc
# and the import machinery don't work anymore
except ImportError:
Copy link
Member

@pablogsal pablogsal Nov 12, 2018

Choose a reason for hiding this comment

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

I think the CI is failing because other exceptions can be propagated from import tracemalloc (if you cange this except ImportError to except Exception as it was before the CI passes).

The error is a bit cryptic because any exception that happens during _formatwarnmsg_impl will trigger an infinite recursion.

Copy link
Member Author

Choose a reason for hiding this comment

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

"except Exception:" is already used multiple times in the function, so yeah, I'm ok to replace "except ImportError: "with "except Exception:". I added a comment.

Copy link
Member

@pablogsal pablogsal left a comment

Choose a reason for hiding this comment

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

LGTM

@vstinner vstinner merged commit 2c07c49 into python:master Nov 13, 2018
@vstinner vstinner deleted the warnings_tracemalloc branch November 13, 2018 01:41
@miss-islington
Copy link
Contributor

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.7.
🐍🍒⛏🤖

@bedevere-bot
Copy link

GH-10509 is a backport of this pull request to the 3.7 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 13, 2018
The warnings module now suggests to enable tracemalloc if the source
is specified, tracemalloc module is available, but tracemalloc is not
tracing memory allocations.
(cherry picked from commit 2c07c49)

Co-authored-by: Victor Stinner <vstinner@redhat.com>
vstinner added a commit that referenced this pull request Dec 4, 2018
The warnings module now suggests to enable tracemalloc if the source
is specified, tracemalloc module is available, but tracemalloc is not
tracing memory allocations.
(cherry picked from commit 2c07c49)

Co-authored-by: Victor Stinner <vstinner@redhat.com>
jnpkrn added a commit to jnpkrn/clufter that referenced this pull request Aug 14, 2019
For some reason, I expected that thefe's an optimizations for modules
already found in cache, hence that respective file descriptors would not
be duplicated needlessly.  I was wrong, as fortunately Python can figure
out on its own these days, and moreover is capable to indicate precisely
where the problem occurred.

Initial messages (./run-tests filter):

/clufter/format.py:807: ResourceWarning: unclosed file
  <_io.TextIOWrapper name='/clufter/tests/XMLFormat-walk/cluster/quorumd/heuristic.py' mode='r' encoding='utf-8'>
  mfile, mpath, mdesc = find_module(name, [root])
ResourceWarning: Enable tracemalloc to get the object allocation traceback
/clufter/filter.py:1066: ResourceWarning: unclosed file
  <_io.TextIOWrapper name='/clufter/tests/XMLFormat-walk/cluster/rm/failoverdomains/failoverdomain.py' mode='r' encoding='utf-8'>
  walk = in_obj.walk_schema(root_dir, preprocess=cls._xslt_preprocess,
ResourceWarning: Enable tracemalloc to get the object allocation traceback

Sample message when tracemalloc enabled:

Object allocated at (most recent call last):
  File "/usr/lib64/python3.8/runpy.py", lineno 192
    return _run_code(code, main_globals, None,
  [14 frames skipped]
  File "/clufter/tests/filter.py", lineno 66
    r = flt.get_template(in_obj, symbol='direct_xslt_test',
  File "/clufter/filter.py", lineno 1066
    walk = in_obj.walk_schema(root_dir, preprocess=cls._xslt_preprocess,
  File "/clufter/format.py", lineno 807
    mfile, mpath, mdesc = find_module(name, [root])
  File "/usr/lib64/python3.8/imp.py", lineno 302
    file = open(file_path, mode, encoding=encoding)

References:
https://docs.python.org/3/whatsnew/3.6.html#warnings
python/cpython#10486

Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
jnpkrn added a commit to jnpkrn/clufter that referenced this pull request Aug 14, 2019
For some reason, I expected that thefe's an optimizations for modules
already found in cache, hence that respective file descriptors would not
be duplicated needlessly.  I was wrong, as fortunately Python can figure
out on its own these days, and moreover is capable to indicate precisely
where the problem occurred.

Initial messages (./run-tests filter):

/clufter/format.py:807: ResourceWarning: unclosed file
  <_io.TextIOWrapper name='/clufter/tests/XMLFormat-walk/cluster/quorumd/heuristic.py' mode='r' encoding='utf-8'>
  mfile, mpath, mdesc = find_module(name, [root])
ResourceWarning: Enable tracemalloc to get the object allocation traceback
/clufter/filter.py:1066: ResourceWarning: unclosed file
  <_io.TextIOWrapper name='/clufter/tests/XMLFormat-walk/cluster/rm/failoverdomains/failoverdomain.py' mode='r' encoding='utf-8'>
  walk = in_obj.walk_schema(root_dir, preprocess=cls._xslt_preprocess,
ResourceWarning: Enable tracemalloc to get the object allocation traceback

Sample message when tracemalloc enabled:

Object allocated at (most recent call last):
  File "/usr/lib64/python3.8/runpy.py", lineno 192
    return _run_code(code, main_globals, None,
  [14 frames skipped]
  File "/clufter/tests/filter.py", lineno 66
    r = flt.get_template(in_obj, symbol='direct_xslt_test',
  File "/clufter/filter.py", lineno 1066
    walk = in_obj.walk_schema(root_dir, preprocess=cls._xslt_preprocess,
  File "/clufter/format.py", lineno 807
    mfile, mpath, mdesc = find_module(name, [root])
  File "/usr/lib64/python3.8/imp.py", lineno 302
    file = open(file_path, mode, encoding=encoding)

References:
https://docs.python.org/3/whatsnew/3.6.html#warnings
python/cpython#10486

Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants