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

Add traceback information to exceptions during docbuild #31005

Closed
tobiasdiez opened this issue Dec 4, 2020 · 34 comments
Closed

Add traceback information to exceptions during docbuild #31005

tobiasdiez opened this issue Dec 4, 2020 · 34 comments

Comments

@tobiasdiez
Copy link
Contributor

The docbuild was failing for me, and it was hard to track down where the original error occurred because there was no (proper) traceback information. In this ticket, such a traceback information is added for exceptions in the docbuild worker following the approach taken in https://bugs.python.org/issue13831.

Example output:

sage_setup.docbuild.utils.RemoteException: 
"""
Traceback (most recent call last):
  File "/mnt/d/Programming/sage/src/sage_setup/docbuild/utils.py", line 179, in run_worker
    result = target(task)
  File "/mnt/d/Programming/sage/src/a_test_docbuild_exc.py", line 7, in target
    1 / 0
ZeroDivisionError: division by zero
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/mnt/d/Programming/sage/src/a_test_docbuild_exc.py", line 12, in <module>
    build_many(target, range(8), processes=8)
  File "/mnt/d/Programming/sage/src/sage_setup/docbuild/utils.py", line 336, in build_many
    raise worker_exc.original_exception
ZeroDivisionError: division by zero

for the test file

from sage_setup.docbuild.utils import build_many

def target(N):
    import time, os, signal
    if N == 1:
        # Task 4 is a poison pill
        1 / 0
    else:
        time.sleep(0.5)
        print('Processed task %s' % N)

build_many(target, range(8), processes=8)

CC: @dimpase @jhpalmieri @seblabbe @slel @orlitzky

Component: documentation

Author: Tobias Diez

Branch/Commit: c37d055

Reviewer: Michael Orlitzky

Issue created by migration from https://trac.sagemath.org/ticket/31005

@tobiasdiez tobiasdiez added this to the sage-9.3 milestone Dec 4, 2020
@tobiasdiez
Copy link
Contributor Author

Author: Tobias Diez

@seblabbe
Copy link
Contributor

seblabbe commented Dec 9, 2020

comment:3

The patchbot reports failures for the files that are changed by the branch:

----------------------------------------------------------------------
sage -t --long --warn-long 40.9 --random-seed=0 src/sage_setup/docbuild/utils.py  # 1 doctest failed
sage -t --long --warn-long 40.9 --random-seed=0 src/sage_setup/docbuild/__init__.py  # 1 doctest failed
----------------------------------------------------------------------

To help the review, may you add an example in the description field of the kind of new information printed in the dochtml.log when an error (warning) occurs?

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jan 9, 2021

Branch pushed to git repo; I updated commit sha1. New commits:

d70709fMerge branch 'develop' of git://github.com/sagemath/sage into public/documentation/add_traceback
f10a30cDon't try to prickle the complete traceback

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jan 9, 2021

Changed commit from 612e0c1 to f10a30c

@tobiasdiez

This comment has been minimized.

@tobiasdiez
Copy link
Contributor Author

comment:5

Thanks for the review. I've now extended the ticket description and fixed the doctests.

@mkoeppe
Copy link
Member

mkoeppe commented Jan 30, 2021

@mkoeppe
Copy link
Member

mkoeppe commented Jan 30, 2021

comment:6

Let's see if this can help shed some light on previously reported docbuild failures such as #30640, #30796, #30825

@jhpalmieri
Copy link
Member

comment:7

I've been having problems building the documentation on OS X Big Sur with the latest Xcode + homebrew. This ticket didn't have any apparent effect on the traceback.

@tobiasdiez
Copy link
Contributor Author

comment:8

@jhpalmieri: So you mean you got an exception in one of the workers, but the traceback still didn't included the proper origin of the exception? That's a bit strange, it's working for me.

@jhpalmieri
Copy link
Member

comment:9

Yes, with the failure in #31344.

@tobiasdiez
Copy link
Contributor Author

comment:10

That makes sense. In #31344, no exception is thrown but the process ends with an error code, which then triggers

        if exitcode != 0:
            raise WorkerDiedException(f"worker for {task[1]} died with non-zero exit code {w.exitcode}")

In this case, no stacktrac is added since this information is simply not available. The worker needs to throw an exception (including a stacktrace) so that the exception handler in

         try:
             result = target(task)
         except BaseException as exc:
             queue.put((None, exc))
             queue.put((None, RemoteExceptionWrapper(exc)))

is invoked. This needs changes to the particular worker, and should be done in a follow-up issue.

@mkoeppe
Copy link
Member

mkoeppe commented Mar 24, 2021

comment:11

Sage development has entered the release candidate phase for 9.3. Setting a new milestone for this ticket based on a cursory review of ticket status, priority, and last modification date.

@mkoeppe mkoeppe modified the milestones: sage-9.3, sage-9.4 Mar 24, 2021
@mkoeppe
Copy link
Member

mkoeppe commented Jul 19, 2021

comment:12

Setting a new milestone for this ticket based on a cursory review.

@mkoeppe mkoeppe modified the milestones: sage-9.4, sage-9.5 Jul 19, 2021
@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Nov 28, 2021

Changed commit from f10a30c to d93695d

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Nov 28, 2021

Branch pushed to git repo; I updated commit sha1. New commits:

d93695dMerge branch 'develop' of https://github.com/sagemath/sage into public/documentation/add_traceback

@mkoeppe
Copy link
Member

mkoeppe commented Nov 28, 2021

@orlitzky
Copy link
Contributor

orlitzky commented Dec 1, 2021

comment:15

Did the GH actions pass? My only suggestion would be to make _rebuild_exc a class method instead of a top-level function, but it's not important. This is a great improvement. Trying to track down all of the docbuild failures with --underscore and --include-tests-blocks is always a nightmare.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Dec 4, 2021

Changed commit from d93695d to 37f1d0c

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Dec 4, 2021

Branch pushed to git repo; I updated commit sha1. New commits:

37f1d0cMove _rebuild_exc and cleanup code

@tobiasdiez
Copy link
Contributor Author

comment:17

Thanks for the review. I've now moved _rebuild_exc to a local method in reduce.
Not sure if the tests passed but patch bot seems to be green.

@orlitzky
Copy link
Contributor

orlitzky commented Dec 6, 2021

comment:18

Replying to @tobiasdiez:

Thanks for the review. I've now moved _rebuild_exc to a local method in reduce.
Not sure if the tests passed but patch bot seems to be green.

Hm, this is even better. But now another refactoring is suggesting itself. Instead of,

def __reduce__(self):
    def _rebuild_exc(exc: BaseException, tb: str):
        """
        Reconstructs the exception, putting the original exception as cause.
        """
        exc.__cause__ = RemoteException(tb)
        return exc

    return _rebuild_exc, (self.exc, self.tb)

couldn't we just do

def __reduce__(self):
    self.exc.__cause__ =  RemoteException(self.tb)
    return self.exc

?

I also think we'll need doctests for the new classes/methods even if they're pretty trivial. Otherwise our coverage goes down.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Dec 6, 2021

Branch pushed to git repo; I updated commit sha1. New commits:

45ef9e3Fix code and add doctest

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Dec 6, 2021

Changed commit from 37f1d0c to 45ef9e3

@tobiasdiez
Copy link
Contributor Author

comment:20

Replying to @orlitzky:

def __reduce__(self):
    self.exc.__cause__ =  RemoteException(self.tb)
    return self.exc

I don't have much experience with pickling, but https://docs.python.org/3/library/pickle.html#object.__reduce__ says that either a string or tuple should be returned.

My solution also didn't worked as __reduce__ cannot return locally defined methods. So I've converted it to a static instance method as you initially suggested.

I also think we'll need doctests for the new classes/methods even if they're pretty trivial. Otherwise our coverage goes down.

I've added a doctest for the reduce method. The other methods are still covered by the existing doctests.

@orlitzky
Copy link
Contributor

comment:21

Replying to @tobiasdiez:

I also think we'll need doctests for the new classes/methods even if they're pretty trivial. Otherwise our coverage goes down.

I've added a doctest for the reduce method. The other methods are still covered by the existing doctests.

I know, I was referring specifically to (before):

$ sage -coverage src/sage_docbuild/utils.py 
------------------------------------------------------------------------
SCORE src/sage_docbuild/utils.py: 50.0% (1 of 2)

Missing documentation:
     * line 10: def __init__(self, message, original_exception=None)
------------------------------------------------------------------------

and (after):

$ sage -coverage src/sage_docbuild/utils.py 
------------------------------------------------------------------------
SCORE src/sage_docbuild/utils.py: 28.6% (2 of 7)

Missing documentation:
     * line 16: def __init__(self, tb:str)
     * line 19: def __str__(self)
     * line 32: def __init__(self, exc:BaseException)
     * line 65: def __init__(self, message:Optional[str], original_exception:Optional[BaseException]=None)

Missing doctests:
     * line 42: def _rebuild_exc(exc:BaseException, tb:str)
------------------------------------------------------------------------

Normally the patchbots would have complained (see the "coverage" plugin), but in this case I think the special nature of the sage_docbuild package avoided it.

I've been on the other side of this argument more than once, so I feel for you, but the developer's guide still states that every function needs a docstring: https://doc.sagemath.org/html/en/developer/coding_basics.html#documentation-strings

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Dec 15, 2021

Changed commit from 45ef9e3 to c37d055

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Dec 15, 2021

Branch pushed to git repo; I updated commit sha1. New commits:

756a79dMerge branch 'develop' of https://github.com/sagemath/sage into public/documentation/add_traceback
c37d055Add docstrings

@tobiasdiez
Copy link
Contributor Author

comment:23

I've added the missing docstrings. But since none of the methods in this class have any tests, I don't have that much motivation to add tests for trivial constructor / properties ;-)

@tobiasdiez
Copy link
Contributor Author

comment:24

Test bot is green, anything else to do?

@orlitzky
Copy link
Contributor

orlitzky commented Jan 4, 2022

comment:25

I forgot what I was waiting for, so it must be OK.

@orlitzky
Copy link
Contributor

orlitzky commented Jan 4, 2022

Reviewer: Michael Orlitzky

@tobiasdiez
Copy link
Contributor Author

comment:26

Thanks!

@mkoeppe mkoeppe modified the milestones: sage-9.5, sage-9.6 Jan 28, 2022
@vbraun
Copy link
Member

vbraun commented Feb 16, 2022

Changed branch from public/documentation/add_traceback to c37d055

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants