You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Peter Valdemar Mørch <sw...@sneakemail.com> on 2007/12/17 10:31:09 UTC

Re: [mp2] mod_perl closes apache's stdin and/or stdout

Hi,

Dmitry Karasik dmitry-at-karasik.eu.org wrote:
> mod_perl during the initialization process closes file descriptor 0,
> and frees it for further reuse in other processes.  Therefore, any cgi script
> executing after mod_perl is initialized, and in the same process tree, will
> have file descriptor 0 closed. The concrete example where this behavior leads
> to impossibility of processing POST requests is reported earlier and is located
> at
> 
>   http://marc.info/?l=apache-modperl&m=119062450730646&w=2

Dmitry's patch from  Sep 25, 2007 
http://www.gossamer-threads.com/lists/modperl/modperl/94912 has still 
not been applied to SVN. May I humbly ask that this patch is applied?

On the dev list 
http://marc.info/?l=apache-modperl-dev&m=119125026607247&w=2 it seems 
that whether or not this really is a bug is drawn into question. I can 
confirm that it, really, really is a bug!

I'm now getting emails from others experiencing the same problem, 
querying for a solution. Dmitry's patch fixes our problem. I'm now 
forwarding links to the patch to other users. Wouldn't it be easier to 
have this or another patch applied or another that also fixes the problem???

The patch didn't apply completely cleanly here against HEAD, but this 
one does ( also at http://demo.capmon.dk/~pvm/modperl_io.patch ) :

~/mod_perl-2.0/src/modules/perl> svn diff
Index: modperl_io.c
===================================================================
--- modperl_io.c        (revision 604795)
+++ modperl_io.c        (working copy)
@@ -116,6 +116,7 @@
      /* if STDIN is open, dup it, to be restored at the end of response */
      if (handle && SvTYPE(handle) == SVt_PVGV &&
          IoTYPE(GvIO(handle)) != IoTYPE_CLOSED) {
+       IO * io = GvIO(handle);
          handle_save = gv_fetchpv(Perl_form(aTHX_
                                             "Apache2::RequestIO::_GEN_%ld",
                                             (long)PL_gensym++),
@@ -128,6 +129,17 @@
              Perl_croak(aTHX_ "Failed to dup STDIN: %" SVf, get_sv("!", 
TRUE));
          }

+       /* In mixed environment of mod_perl and cgi scripts, cgi
+        * scripts may read content of POST requests off
+        * STDIN. do_close() calls actual close(0), freeing the
+        * descriptor 0 for reuse, and creating havoc for anyone
+        * reading from file descriptor 0.  This hack changes the IO
+        * type to IoTYPE_STD, because do_close() does not call
+        * underlying close() on IO handles of these types, but does
+        * free the associated resources. */
+        if ( IoIFP(io) && PerlIO_fileno(IoIFP(io)) == 0)
+           IoTYPE(io) = IoTYPE_STD;
+
          /* similar to PerlIO::scalar, the PerlIO::Apache layer doesn't
           * have file descriptors, so STDIN must be closed before it can
           * be reopened */

Thanks,

Peter
-- 
Peter Valdemar Mørch
http://www.morch.com

Re: [mp2] mod_perl closes apache's stdin and/or stdout

Posted by Peter Valdemar Mørch <sw...@sneakemail.com>.
Torsten Foertsch torsten.foertsch-at-gmx.net |Lists| wrote:
> I think Dmitrys argument about file descriptors being a shared/global resource 
> and hence must not be closed is very strong. But on the other hand existing 
> tests must not be broken. (or otherwise clearly explained why the test is 
> buggy) And of course a test that demonstate the bug is necessary.

Thanks for your input.

I'll ask Dmitry if he'll provide relevant test case coverage so that the 
patch can be applied. It may be a little while, though, christmas and 
all. Just wanted to post that this is still breathing and that I now 
realize we have the ball.

Peter

-- 
Peter Valdemar Mørch
http://www.morch.com

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


Re: [mp2] mod_perl closes apache's stdin and/or stdout

Posted by Torsten Foertsch <to...@gmx.net>.
On Tue 18 Dec 2007, Philippe M. Chiasson wrote:
> >> Dmitry's patch from  Sep 25, 2007
> >> http://www.gossamer-threads.com/lists/modperl/modperl/94912 has still
> >> not been applied to SVN. May I humbly ask that this patch is applied?
> >
> > I remember that Toersten and I put together a unit test for this but I
> > don't think there was any change in behavior with this patch, so I'm not
> > sure if anyone was able to demonstrate that it changed the behavior.
>
> It certainly breaks t/modperl/io_nested_with_closed_stds
> [error] [client 127.0.0.1] Filehandle STDOUT reopened as STDIN only for
> input at t/response/TestModperl/io_nested_with_closed_stds.pm line 49.\n
>
> > If you have the tuits and could track down that unit test, show that it
> > exercises the problem in question, we probably wouldn't need much else
> > to apply this patch.  I'd try to do this but am overextended on my tuits
> > right now.
>
> Yup, without a test case that correctly demonstrates the bug, and a patch
> that doesn't break existing tests, not very likely to get applied.

This patch is something I wanted to investigate more thoroughly but as ever 
there is no time left for interesting problems.

I think Dmitrys argument about file descriptors being a shared/global resource 
and hence must not be closed is very strong. But on the other hand existing 
tests must not be broken. (or otherwise clearly explained why the test is 
buggy) And of course a test that demonstate the bug is necessary.

Torsten

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


Re: [mp2] mod_perl closes apache's stdin and/or stdout

Posted by "Philippe M. Chiasson" <go...@ectoplasm.org>.
Fred Moyer wrote:
> Peter Valdemar Mørch wrote:
>> Hi,
>>
>> Dmitry Karasik dmitry-at-karasik.eu.org wrote:
>>> mod_perl during the initialization process closes file descriptor 0,
>>> and frees it for further reuse in other processes.  Therefore, any cgi 
>>> script
>>> executing after mod_perl is initialized, and in the same process tree, 
>>> will
>>> have file descriptor 0 closed. The concrete example where this 
>>> behavior leads
>>> to impossibility of processing POST requests is reported earlier and 
>>> is located
>>> at
>>>
>>>   http://marc.info/?l=apache-modperl&m=119062450730646&w=2
>> Dmitry's patch from  Sep 25, 2007 
>> http://www.gossamer-threads.com/lists/modperl/modperl/94912 has still 
>> not been applied to SVN. May I humbly ask that this patch is applied?
> 
> I remember that Toersten and I put together a unit test for this but I 
> don't think there was any change in behavior with this patch, so I'm not 
> sure if anyone was able to demonstrate that it changed the behavior.

It certainly breaks t/modperl/io_nested_with_closed_stds
[error] [client 127.0.0.1] Filehandle STDOUT reopened as STDIN only for input at t/response/TestModperl/io_nested_with_closed_stds.pm line 49.\n

> If you have the tuits and could track down that unit test, show that it 
> exercises the problem in question, we probably wouldn't need much else 
> to apply this patch.  I'd try to do this but am overextended on my tuits 
> right now.

Yup, without a test case that correctly demonstrates the bug, and a patch
that doesn't break existing tests, not very likely to get applied.

-- 
Philippe M. Chiasson     GPG: F9BFE0C2480E7680 1AE53631CB32A107 88C3A5A5
http://gozer.ectoplasm.org/       m/gozer\@(apache|cpan|ectoplasm)\.org/


Re: [mp2] mod_perl closes apache's stdin and/or stdout

Posted by "Philippe M. Chiasson" <go...@ectoplasm.org>.
Fred Moyer wrote:
> Peter Valdemar Mørch wrote:
>> Hi,
>>
>> Dmitry Karasik dmitry-at-karasik.eu.org wrote:
>>> mod_perl during the initialization process closes file descriptor 0,
>>> and frees it for further reuse in other processes.  Therefore, any cgi 
>>> script
>>> executing after mod_perl is initialized, and in the same process tree, 
>>> will
>>> have file descriptor 0 closed. The concrete example where this 
>>> behavior leads
>>> to impossibility of processing POST requests is reported earlier and 
>>> is located
>>> at
>>>
>>>   http://marc.info/?l=apache-modperl&m=119062450730646&w=2
>> Dmitry's patch from  Sep 25, 2007 
>> http://www.gossamer-threads.com/lists/modperl/modperl/94912 has still 
>> not been applied to SVN. May I humbly ask that this patch is applied?
> 
> I remember that Toersten and I put together a unit test for this but I 
> don't think there was any change in behavior with this patch, so I'm not 
> sure if anyone was able to demonstrate that it changed the behavior.

It certainly breaks t/modperl/io_nested_with_closed_stds
[error] [client 127.0.0.1] Filehandle STDOUT reopened as STDIN only for input at t/response/TestModperl/io_nested_with_closed_stds.pm line 49.\n

> If you have the tuits and could track down that unit test, show that it 
> exercises the problem in question, we probably wouldn't need much else 
> to apply this patch.  I'd try to do this but am overextended on my tuits 
> right now.

Yup, without a test case that correctly demonstrates the bug, and a patch
that doesn't break existing tests, not very likely to get applied.

-- 
Philippe M. Chiasson     GPG: F9BFE0C2480E7680 1AE53631CB32A107 88C3A5A5
http://gozer.ectoplasm.org/       m/gozer\@(apache|cpan|ectoplasm)\.org/


Re: [mp2] mod_perl closes apache's stdin and/or stdout

Posted by Fred Moyer <fr...@redhotpenguin.com>.
Peter Valdemar Mørch wrote:
> Hi,
> 
> Dmitry Karasik dmitry-at-karasik.eu.org wrote:
>> mod_perl during the initialization process closes file descriptor 0,
>> and frees it for further reuse in other processes.  Therefore, any cgi 
>> script
>> executing after mod_perl is initialized, and in the same process tree, 
>> will
>> have file descriptor 0 closed. The concrete example where this 
>> behavior leads
>> to impossibility of processing POST requests is reported earlier and 
>> is located
>> at
>>
>>   http://marc.info/?l=apache-modperl&m=119062450730646&w=2
> 
> Dmitry's patch from  Sep 25, 2007 
> http://www.gossamer-threads.com/lists/modperl/modperl/94912 has still 
> not been applied to SVN. May I humbly ask that this patch is applied?

I remember that Toersten and I put together a unit test for this but I 
don't think there was any change in behavior with this patch, so I'm not 
sure if anyone was able to demonstrate that it changed the behavior.

If you have the tuits and could track down that unit test, show that it 
exercises the problem in question, we probably wouldn't need much else 
to apply this patch.  I'd try to do this but am overextended on my tuits 
right now.

> 
> On the dev list 
> http://marc.info/?l=apache-modperl-dev&m=119125026607247&w=2 it seems 
> that whether or not this really is a bug is drawn into question. I can 
> confirm that it, really, really is a bug!
> 
> I'm now getting emails from others experiencing the same problem, 
> querying for a solution. Dmitry's patch fixes our problem. I'm now 
> forwarding links to the patch to other users. Wouldn't it be easier to 
> have this or another patch applied or another that also fixes the 
> problem???
> 
> The patch didn't apply completely cleanly here against HEAD, but this 
> one does ( also at http://demo.capmon.dk/~pvm/modperl_io.patch ) :
> 
> ~/mod_perl-2.0/src/modules/perl> svn diff
> Index: modperl_io.c
> ===================================================================
> --- modperl_io.c        (revision 604795)
> +++ modperl_io.c        (working copy)
> @@ -116,6 +116,7 @@
>      /* if STDIN is open, dup it, to be restored at the end of response */
>      if (handle && SvTYPE(handle) == SVt_PVGV &&
>          IoTYPE(GvIO(handle)) != IoTYPE_CLOSED) {
> +       IO * io = GvIO(handle);
>          handle_save = gv_fetchpv(Perl_form(aTHX_
>                                             "Apache2::RequestIO::_GEN_%ld",
>                                             (long)PL_gensym++),
> @@ -128,6 +129,17 @@
>              Perl_croak(aTHX_ "Failed to dup STDIN: %" SVf, get_sv("!", 
> TRUE));
>          }
> 
> +       /* In mixed environment of mod_perl and cgi scripts, cgi
> +        * scripts may read content of POST requests off
> +        * STDIN. do_close() calls actual close(0), freeing the
> +        * descriptor 0 for reuse, and creating havoc for anyone
> +        * reading from file descriptor 0.  This hack changes the IO
> +        * type to IoTYPE_STD, because do_close() does not call
> +        * underlying close() on IO handles of these types, but does
> +        * free the associated resources. */
> +        if ( IoIFP(io) && PerlIO_fileno(IoIFP(io)) == 0)
> +           IoTYPE(io) = IoTYPE_STD;
> +
>          /* similar to PerlIO::scalar, the PerlIO::Apache layer doesn't
>           * have file descriptors, so STDIN must be closed before it can
>           * be reopened */
> 
> Thanks,
> 
> Peter