Ejemplo n.º 1
0
    def test_locals_collected_when_greenlet_dead_but_still_referenced(self):
        # https://github.com/gevent/gevent/issues/387
        import gevent

        my_local = MyLocal()
        my_local.sentinel = None
        greentest.gc_collect_if_needed()

        del created_sentinels[:]
        del deleted_sentinels[:]

        def demonstrate_my_local():
            # Get the important parts
            getattr(my_local, 'sentinel')

        # Create and reference greenlets
        greenlets = [Thread(target=demonstrate_my_local) for _ in range(5)]
        for t in greenlets:
            t.start()
        gevent.sleep()

        self.assertEqual(len(created_sentinels), len(greenlets))

        for g in greenlets:
            assert not g.is_alive()
        gevent.sleep() # let the callbacks run
        greentest.gc_collect_if_needed()

        # The sentinels should be gone too
        self.assertEqual(len(deleted_sentinels), len(greenlets))
Ejemplo n.º 2
0
        def accept_once(listener):
            # delete/overwrite the original conn
            # object, only keeping the file object around
            # closing the file object should close everything

            # This is not *exactly* true on Python 3. This produces
            # a ResourceWarning, which we silence below. (Previously we actually
            # *saved* a reference to the socket object, so we
            # weren't testing what we thought we were.)

            # It's definitely not true on PyPy, which needs GC to
            # reliably close everything; sometimes this is more than
            # one collection cycle. And PyPy issues a warning with -X
            # track-resources that we cannot catch.
            with warnings.catch_warnings():
                warnings.simplefilter('ignore')

                try:
                    conn = listener.accept()[0]
                    # Note that we overwrite the original variable,
                    # losing our reference to the socket.
                    conn = conn.makefile(mode='wb')
                    conn.write(b'hello\n')
                    conn.close()
                    _write_to_closed(conn, b'a')
                finally:
                    listener.close()
                    del listener
                    del conn
                    gc_collect_if_needed()
                    gc_collect_if_needed()
Ejemplo n.º 3
0
    def test_locals_collected_when_greenlet_dead_but_still_referenced(self):
        # https://github.com/gevent/gevent/issues/387
        import gevent

        my_local = MyLocal()
        my_local.sentinel = None
        greentest.gc_collect_if_needed()

        del created_sentinels[:]
        del deleted_sentinels[:]

        def demonstrate_my_local():
            # Get the important parts
            getattr(my_local, 'sentinel')

        # Create and reference greenlets
        greenlets = [Thread(target=demonstrate_my_local) for _ in range(5)]
        for t in greenlets:
            t.start()
        gevent.sleep()

        self.assertEqual(len(created_sentinels), len(greenlets))

        for g in greenlets:
            assert not g.is_alive()
        gevent.sleep() # let the callbacks run
        greentest.gc_collect_if_needed()

        # The sentinels should be gone too
        self.assertEqual(len(deleted_sentinels), len(greenlets))
Ejemplo n.º 4
0
        def accept_once(listener):
            # delete/overwrite the original conn
            # object, only keeping the file object around
            # closing the file object should close everything

            # This is not *exactly* true on Python 3. This produces
            # a ResourceWarning, which we silence below. (Previously we actually
            # *saved* a reference to the socket object, so we
            # weren't testing what we thought we were.)

            # It's definitely not true on PyPy, which needs GC to
            # reliably close everything; sometimes this is more than
            # one collection cycle. And PyPy issues a warning with -X
            # track-resources that we cannot catch.
            with warnings.catch_warnings():
                warnings.simplefilter('ignore')

                try:
                    conn = listener.accept()[0]
                    # Note that we overwrite the original variable,
                    # losing our reference to the socket.
                    conn = conn.makefile(mode='wb')
                    conn.write(b'hello\n')
                    conn.close()
                    _write_to_closed(conn, b'a')
                finally:
                    listener.close()
                    del listener
                    del conn
                    gc_collect_if_needed()
                    gc_collect_if_needed()
Ejemplo n.º 5
0
 def run_and_check(self, run_client):
     wref_to_hidden_server_socket = self.run_interaction(run_client=run_client)
     greentest.gc_collect_if_needed()
     if wref_to_hidden_server_socket():
         from pprint import pformat
         print(pformat(gc.get_referrers(wref_to_hidden_server_socket())))
         for x in gc.get_referrers(wref_to_hidden_server_socket()):
             print(pformat(x))
             for y in gc.get_referrers(x):
                 print('-', pformat(y))
         self.fail('server socket should be dead by now')
Ejemplo n.º 6
0
 def run_and_check(self, run_client):
     wref_to_hidden_server_socket = self.run_interaction(
         run_client=run_client)
     greentest.gc_collect_if_needed()
     if wref_to_hidden_server_socket():
         from pprint import pformat
         print(pformat(gc.get_referrers(wref_to_hidden_server_socket())))
         for x in gc.get_referrers(wref_to_hidden_server_socket()):
             print(pformat(x))
             for y in gc.get_referrers(x):
                 print('-', pformat(y))
         self.fail('server socket should be dead by now')