Skip to content
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

BUG: reference count leak when using THPLayout_New and THPMemoryFormat_New (static analyzer reports) #77839

Open
Snape3058 opened this issue May 19, 2022 · 1 comment
Labels
module: memory usage PyTorch is using more memory than it should, or it is leaking memory module: python frontend For issues relating to PyTorch's Python frontend triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@Snape3058
Copy link

Snape3058 commented May 19, 2022

Hint 1: In function THPLayout_New and THPMemoryFormat_New, the tp_alloc method is used to allocate the memory for the PyObject to be returned. If the default allocator function is inherited, the function call will return a new reference.

Hint 2: Function PyModule_AddObject will steal a reference to the third argument only when the return value is zero.


  • In function initializeLayouts, the trigger path provided by our analyzer is as follows. (Internal Report ID: e2a925)
  1. A new reference is returned from THPLayout_New and assigned to strided_layout. (refcnt = 1)

    PyObject* strided_layout = THPLayout_New(at::Layout::Strided, "torch.strided");

  2. Increase the refcnt. (refcnt = 2)

    Py_INCREF(strided_layout);

  3. An exception is thrown without decreasing the refcnt.

    throw python_error();


  • In lambda expression add_memory_format in function initializeMemoryFormats, the trigger path provided by our analyzer is as follows. (Internal Report ID: 09d8df)
  1. A new reference is returned from THPMemoryFormat_New and assigned to memory_format. (refcnt = 1)

    PyObject* memory_format = THPMemoryFormat_New(format, module_name + name);

  2. Increase the refcnt. (refcnt = 2)

    Py_INCREF(memory_format);

  3. Decrease the refcnt. (refcnt = 1)

    Py_DECREF(memory_format);

  4. An exception is thrown without decreasing the refcnt.

cc @ezyang @gchanan @zou3519

@albanD
Copy link
Collaborator

albanD commented May 19, 2022

cc @ezyang

@albanD albanD added high priority triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module module: python frontend For issues relating to PyTorch's Python frontend labels May 19, 2022
@mruberry mruberry added the module: memory usage PyTorch is using more memory than it should, or it is leaking memory label May 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: memory usage PyTorch is using more memory than it should, or it is leaking memory module: python frontend For issues relating to PyTorch's Python frontend triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

No branches or pull requests

4 participants