[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

Re: Avoinding file handle leak using the Python bindings & core.Stream

From: Ulrich Eckhardt <ulrich.eckhardt_at_dominolaser.com>
Date: Mon, 16 Apr 2012 12:22:29 +0200

Am 16.04.2012 11:29, schrieb Willmer, Alex (PTS):
> I've been working on an full text search plugin for Trac.

Nice! ;)

> At initial setup this indexes the entire Subversion repository by
> reading every node of every version. During testing we discovered
> that the indexer was running out of file handles, due to a file
> handle leak. As far as I can tell each core.Stream(fs.file_contents(.))
> instance that was created and not subsequently .read() left an
> unclosed file handle. To work around this I have monkey patched a
> Stream.close() method that calls svn_stream_close, which is used
> in a try/finally block.

Disclaimer: I'm not really familiar with the SVN/Python bindings.
However, concerning Python in general, explicitly calling close() is the
wrong way. Instead you should make sure files are closed by opening them
in a with clause:

  with open(...) as f:
      ... # use f

This will make sure that 'f' gets a C++-like scope and is automatically
closed on exit of that scope, regardless whether the exit is due to an
exception or a "normal" exit. You can easily add this behaviour by
creating an __entry__/__exit__ function, look up "context manager" on
the web.

That said, when the core.Stream instance is garbage-collected (normally
when it goes out of scope), it also releases its "self._stream" which
can subsequently be garbage-collected. If this object in turn requires
explicit cleanup of its internal resources, it should provide a __del__
method doing that. If it leaks this a file handle in the (unusual?) case
that the content wasn't read, that is the place where the bug actually is.

> The work-around has fixed our file-handle leak for, but I believe it
> points to a bug in the Subversion bindings for which I'll try and
> provide a patch. Before I file a bug I'd like to check I haven't
> misunderstood anything:
> 1. In the Python bindings core.Stream doesn't have a .close()
> method [a]. Is there any reason this might be intentional?

I guess that yes. The point is that the file interface only has read()
and write(), but not close(). In other words, functions that are
supposed to work with files and file-like types should only expect those
two functions, not e.g. a close().

I can't comment on your second question, I'm not familiar enough with
the API.

Good luck!

Uli

**************************************************************************************
Domino Laser GmbH, Fangdieckstraße 75a, 22547 Hamburg, Deutschland
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
**************************************************************************************
Visit our website at http://www.dominolaser.com
**************************************************************************************
Diese E-Mail einschließlich sämtlicher Anhänge ist nur für den Adressaten bestimmt und kann vertrauliche Informationen enthalten. Bitte benachrichtigen Sie den Absender umgehend, falls Sie nicht der beabsichtigte Empfänger sein sollten. Die E-Mail ist in diesem Fall zu löschen und darf weder gelesen, weitergeleitet, veröffentlicht oder anderweitig benutzt werden.
E-Mails können durch Dritte gelesen werden und Viren sowie nichtautorisierte Änderungen enthalten. Domino Laser GmbH ist für diese Folgen nicht verantwortlich.
**************************************************************************************
Received on 2012-04-16 12:23:16 CEST

This is an archived mail posted to the Subversion Users mailing list.

This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.