[lldb-dev] Python object lifetimes affect the reliability of tests

Todd Fiala via lldb-dev lldb-dev at lists.llvm.org
Thu Oct 15 09:29:17 PDT 2015


On Thu, Oct 15, 2015 at 8:50 AM, Adrian McCarthy via lldb-dev <
lldb-dev at lists.llvm.org> wrote:

> I've tracked down a source of flakiness in tests on Windows to Python
> object lifetimes and the SB interface, and I'm wondering how best to handle
> it.
>
> Consider this portion of a test from TestTargetAPI:
>
>     def find_functions(self, exe_name):
>         """Exercise SBTaget.FindFunctions() API."""
>         exe = os.path.join(os.getcwd(), exe_name)
>
>         # Create a target by the debugger.
>         target = self.dbg.CreateTarget(exe)
>         self.assertTrue(target, VALID_TARGET)
>         list = target.FindFunctions('c', lldb.eFunctionNameTypeAuto)
>         self.assertTrue(list.GetSize() == 1)
>
>         for sc in list:
>             self.assertTrue(sc.GetModule().GetFileSpec().GetFilename() ==
> exe_name)
>             self.assertTrue(sc.GetSymbol().GetName() == 'c')
>
> The local variables go out of scope when the function exits, but the SB
> (C++) objects they represent aren't (always) immediately destroyed.  At
> least some of these objects keep references to the executable module in the
> shared module list, so when the test framework cleans up and calls
> `SBDebugger::DeleteTarget`, the module isn't orphaned, so LLDB maintains an
> open handle to the executable.
>
> The result of the lingering handle is that, when the next test case in the
> test suite tries to re-build the executable, it fails because the file is
> not writable.  (This is problematic on Windows because the file system
> works differently in this regard than Unix derivatives.)  Every subsequent
> case in the test suite fails.
>
> I managed to make the test work reliably by rewriting it like this:
>
>     def find_functions(self, exe_name):
>         """Exercise SBTaget.FindFunctions() API."""
>         exe = os.path.join(os.getcwd(), exe_name)
>
>         # Create a target by the debugger.
>         target = self.dbg.CreateTarget(exe)
>         self.assertTrue(target, VALID_TARGET)
>
>         try:
>             list = target.FindFunctions('c', lldb.eFunctionNameTypeAuto)
>             self.assertTrue(list.GetSize() == 1)
>
>             for sc in list:
>                 try:
>
> self.assertTrue(sc.GetModule().GetFileSpec().GetFilename() == exe_name)
>                     self.assertTrue(sc.GetSymbol().GetName() == 'c')
>                 finally:
>                     del sc
>
>         finally:
>             del list
>
> The finally blocks ensure that the corresponding C++ objects are
> destroyed, even if the function exits as a result of a Python exception
> (e.g., if one of the assertion expressions is false and the code throws an
> exception).  Since the objects are destroyed, the reference counts are back
> to where they should be, and the orphaned module is closed when the target
> is deleted.
>
> But this is ugly and maintaining it would be error prone.  Is there a
> better way to address this?
>
>
We should drill into why they're not cleaning up right away.  If it's out
of our control (i.e. some kind of lazy clean up), then maybe we can put in
some meta-magic that generates the deletes like you added.


> In general, it seems bad that our tests aren't well-isolated.
>

Each .py file is getting run as a separate process unless the tests are run
in a way that disables the concurrent test runner.  Methods within a given
.py file are getting run in the same process.  So we're really talking
about test methods within a file, not test methods across files.


> I sympathize with the concern that re-starting LLDB for each test case
> would slow down testing, but I'm also concerned that the state of LLDB for
> any given test case can depend on what happened in the earlier cases.
>
>
One thing I plan to do mid-term is:
* announce all the tests that can run in a python file
* ensure we report on tests *not* run because of a crash (i.e. get counts
for true unknowns, not just info on tests that pass/fail/error).
* having an "announce-only" pass so we can dynamically collect up all he
tests without necessarily running them.  (for things like test runners that
can list all the tests available and want to show progress).

This would conceivably support the underpinnings to say:
* collect all the tests (announce-only)
* add a way to fire up a single test method (we sortta have that already),
and only do one test method per process.

I think there is additional coordination that would have to happen to
handle inferior binaries build for a test and make sure they exist where
(and when) they need to, with the right arch settings and whatnot.

But, that would definitely get full isolation per test method.


> Adrian.
>
> _______________________________________________
> lldb-dev mailing list
> lldb-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-dev
>
>


-- 
-Todd
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/lldb-dev/attachments/20151015/1c533a8e/attachment.html>


More information about the lldb-dev mailing list