You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Stefan Bodewig <bo...@apache.org> on 2016/06/13 09:19:57 UTC

[compress] change behavior of bzip-output finalize?

Hi all

I'm trying to bring a discussion from COMPRESS-357 over to the list.

The bzip2 output stream has a finish method that is used to ensure all
data has been written to the stream and a separate close method that
invokes finish. And it overrides finalize to invoke finish in case
people have forgotten to close the stream - this behavior has been
present in the very first release of [compress].

The discussion in COMPRESS-357 shows that we'd need to synchronize - or
make volatile - quite a bit of code inside of
BZip2CompressorOutputStream for finalize to work properly. The
alternative is to simply no longer invoke finish from finalize (but log
an error if an unclosed stream is detected). This is a change that
breaks backwards compatibility but most likely only for people who are
doing something wrong. It may be OK to change it if the change is
advertized properly as it shouldn't affect too many people anyway.

There is one other instance of finalize doing the same - ZipFile. Here I
remember why it is the case, Ant uses its version of ZipFile inside of
its classloader and keeps the ZipFile instances open until the
classloader is disposed of - it might be possible to invoke close on the
ZipFile instances in the classloader's finalize but back then it looked
easier that way.

In the case of ZipFile I'd argue the only field that needs to be
published safely apart fromt the volatile closed flag is "archive" which
is final, so we should be safe.

Cheers

        Stefan

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [compress] change behavior of bzip-output finalize?

Posted by Torsten Curdt <tc...@vafer.org>.
>
> AIUI finalize() is not guaranteed to be called, so any code that
> relies on it is liable to be disappointed from time to time.
>
> IMO at best one can use finalize() - *if* indeed it gets called - to
> warn users that they have not closed a resource.
>

+1

Re: [compress] change behavior of bzip-output finalize?

Posted by sebb <se...@gmail.com>.
On 13 June 2016 at 10:19, Stefan Bodewig <bo...@apache.org> wrote:
> Hi all
>
> I'm trying to bring a discussion from COMPRESS-357 over to the list.
>
> The bzip2 output stream has a finish method that is used to ensure all
> data has been written to the stream and a separate close method that
> invokes finish. And it overrides finalize to invoke finish in case
> people have forgotten to close the stream - this behavior has been
> present in the very first release of [compress].
>
> The discussion in COMPRESS-357 shows that we'd need to synchronize - or
> make volatile - quite a bit of code inside of
> BZip2CompressorOutputStream for finalize to work properly. The
> alternative is to simply no longer invoke finish from finalize (but log
> an error if an unclosed stream is detected). This is a change that
> breaks backwards compatibility but most likely only for people who are
> doing something wrong. It may be OK to change it if the change is
> advertized properly as it shouldn't affect too many people anyway.
>
> There is one other instance of finalize doing the same - ZipFile. Here I
> remember why it is the case, Ant uses its version of ZipFile inside of
> its classloader and keeps the ZipFile instances open until the
> classloader is disposed of - it might be possible to invoke close on the
> ZipFile instances in the classloader's finalize but back then it looked
> easier that way.
>
> In the case of ZipFile I'd argue the only field that needs to be
> published safely apart fromt the volatile closed flag is "archive" which
> is final, so we should be safe.

AIUI finalize() is not guaranteed to be called, so any code that
relies on it is liable to be disappointed from time to time.

IMO at best one can use finalize() - *if* indeed it gets called - to
warn users that they have not closed a resource.

> Cheers
>
>         Stefan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [compress] change behavior of bzip-output finalize?

Posted by Stefan Bodewig <bo...@apache.org>.
On 2016-06-13, Stefan Bodewig wrote:

> The bzip2 output stream has a finish method that is used to ensure all
> data has been written to the stream and a separate close method that
> invokes finish. And it overrides finalize to invoke finish in case
> people have forgotten to close the stream - this behavior has been
> present in the very first release of [compress].

It now no longer does. I've updated the changes report to mark the
backwards incompatible change.

Thanks for your advice

       Stefan

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org