apport_python_hook: Crashes if os.getcwd() throws FileNotFoundError

Bug #1977954 reported by Benjamin Drung
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Apport
Fix Released
Medium
Unassigned
apport (Ubuntu)
Fix Released
Medium
Unassigned
Jammy
New
Medium
Unassigned

Bug Description

Impact
------

The Apport Python hook should be transparent to the user (which is normally the case). Bugs and crashes inside the hook lead to an exception inside the exception which clutters the output and confuse the user. User/Developer need to understand this Python hook and look into the encapsulated stack trace only.

Test Case
---------

1. Create /tmp/getcwd.py with following content:
```
#!/usr/bin/python3

import os
import tempfile

tempdir = tempfile.mkdtemp()
os.chdir(tempdir)
os.rmdir(tempdir)
os.getcwd()
```
2. Make it executable and execute it.

It should produce following output:

```
Traceback (most recent call last):
  File "/tmp/getcwd.py", line 9, in <module>
    os.getcwd()
FileNotFoundError: [Errno 2] No such file or directory
```

instead of:

```
$ /tmp/getcwd.py
Traceback (most recent call last):
  File "/tmp/getcwd.py", line 9, in <module>
    os.getcwd()
FileNotFoundError: [Errno 2] No such file or directory
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 76, in apport_excepthook
    binary = os.path.realpath(os.path.join(os.getcwd(), sys.argv[0]))
FileNotFoundError: [Errno 2] No such file or directory

Original exception was:
Traceback (most recent call last):
  File "/tmp/getcwd.py", line 9, in <module>
    os.getcwd()
FileNotFoundError: [Errno 2] No such file or directory
```

Another related issue: bug #1979637

Regression Potential
--------------------

There might be a regression that causes capturing Python exception fail. Then reporting them as bugs to Launchpad or the error tracker would not be possible.

Benjamin Drung (bdrung)
Changed in apport:
milestone: none → 2.22.0
status: New → Triaged
importance: Undecided → Medium
Benjamin Drung (bdrung)
description: updated
Benjamin Drung (bdrung)
description: updated
description: updated
Benjamin Drung (bdrung)
description: updated
description: updated
Revision history for this message
Benjamin Drung (bdrung) wrote :
Changed in apport:
status: Triaged → Fix Committed
Benjamin Drung (bdrung)
Changed in apport:
status: Fix Committed → Fix Released
Revision history for this message
Launchpad Janitor (janitor) wrote :

This bug was fixed in the package apport - 2.22.0-0ubuntu1

---------------
apport (2.22.0-0ubuntu1) kinetic; urgency=medium

  * New upstream release:
    - Replace `ProblemReport.get_date` by `ProblemReport.get_timestamp`, fixes
      regression in 2.21.0 (LP: #1978487)
    - apport-gtk: Exclude trailing dot from URL links (LP: #1978301)
    - Fix `AttributeError`: 'NoneType' object has no attribute 'write', fixes
      regression in 2.21.0 (LP: #1979211)
    - apport_python_hook: Properly handle missing modules (LP: #1774843)
    - apport_python_hook: Fix FileNotFoundError if the current directory was
      deleted (LP: #1979637)
    - apport_python_hook: Fix crash if os.getcwd() fails (LP: #1977954)
    - Replace deprecated 'imp' module (LP: #1947425)
    - whoopsie-upload-all: Fix 'EOFError' object has no attribute 'errno',
      fixes regression in 2.21.0 (LP: #1979681)
  * Fix KeyError: 'CasperMD5json'

 -- Benjamin Drung <email address hidden> Mon, 27 Jun 2022 12:03:00 +0200

Changed in apport (Ubuntu):
status: New → Fix Released
Benjamin Drung (bdrung)
description: updated
Benjamin Drung (bdrung)
description: updated
Revision history for this message
Steve Langasek (vorlon) wrote : Proposed package upload rejected

An upload of apport to jammy-proposed has been rejected from the upload queue for the following reason: "contains bugfixes without bug references or test cases, does not meet SRU policy".

Benjamin Drung (bdrung)
Changed in apport (Ubuntu):
importance: Undecided → Medium
Changed in apport (Ubuntu Jammy):
importance: Undecided → Medium
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.