217 Commits

Author SHA1 Message Date
Fabrizio Gennari 2c9bcd0605 Check whether *value is indeed an SvRV before casting it 2015-06-30 23:54:40 +02:00
protoCall7 8a1f6c440a Changed abs to labs because offset is declared as a long 2015-06-01 16:50:20 -07:00
James Raspass 0389748d4a plugins/psgi - use PL_sv_{yes,no} in env
Use PL_sv_yes & PL_sv_no instead of newSViv(1) & newSViv(0) for bools
as they're interpreter globals that don't need to be created everytime.

Added a test and refactored the tests that were added with a1e30157,
all .t files in t/perl are now ran under all major perl versions with
and without threading when t/perl/run is invoked.

On my box, psgi_plugin.o went from 62,296 bytes to 62,208 bytes. Not a
huge saving I'll grant you, but every little helps :-)
2015-04-17 08:50:05 +01:00
unbit 334baa8050 Merge pull request #886 from funkybob/feature/cleanup
Tidy with flake8
2015-04-13 06:42:03 +02:00
James Raspass a1e3015783 plugins/psgi - remove the need for Scalar::Util
Commit ff9bab4b fixed support for sending IO like handles or GLOBs
via sendfile by first calling fileno on them. This was checked
by calling Scalar::Util::reftype on the reference first, as per
https://metacpan.org/pod/PSGI#Body . This may be sane in pure
perl, but there are better methods in XS/C than string evalling
some Perl, which will eventually also call some XS/C.

This commit introduces more robust checking of whether we have a
"real" filehandle, and also introduces a test that chucks a bunch
of different scalars and references at uWSGI to make sure it doesn't
choke. I wasn't sure how to hook this test up to travis, or wether
that's even desireable, so just left the test in what I believe to
be the right directories.

Because this commit massively simplifies the code, it also had an
effect on object size, on my machine (x64, GCC 4.9), psgi_plugin.o
went from 63360 bytes to 62296 bytes.

The indentation level of the ->getline for loop has been left at
its original level, which is now incorrect, to make the diff
easier to read. A whitespace cleanup commit could follow.

In related news, does anyone know the purpose of the three other
use lines in psgi_loader.c, two of them were added back in 2011
by cf08972e, but I can't work out what their inclusion has to do
with memleak hunting.

Basically I would love to remove these lines as I don't believe
they're needed for uWSGI to function, and if apps running under
uWSGI need them, they should load them themselves.

Also related, would more PSGI refactorings be welcome? I get the
impression that Perl is a bit of a second class citizen in uWSGI
these days, and as both an avid user of uWSGI+PSGI and a hobby XS
developer, I'd love to clean up more of the PSGI plugin.
2015-04-11 23:16:27 +01:00
Curtis 057cfe761f Tidy with flake8 2015-04-11 22:50:11 +10:00
unbit da3067e9ae Merge pull request #839 from ikruglov/master
psgi_loader: fix a one off bug in XS_input_seek()
2015-03-03 15:39:34 +01:00
Ivan Kruglov d7158ccb23 psgi_loader: make sure that at least two params are passed to XS_input_seek() 2015-02-13 14:20:19 +01:00
Ivan Kruglov 933d7e7cba psgi_loader: fix a one off bug in XS_input_seek() 2015-02-13 13:11:02 +01:00
Unbit 11b0e431f4 attempt to fix #833 2015-02-11 11:37:54 +01:00
Unbit c6182dd539 fix return value for hv_store in uwsgi::opt 2015-02-08 17:34:06 +01:00
Aarni Koskela 91a04dd461 Add reasons to worker self-shutdown messages.
This should make it easier to understand why a worker is shutting down.

* Modify `goodbye_cruel_world` to accept a `reason` format string and
  varargs.
* Modify calls to the above function to include a verbose reason string.
* Modify the `gbcw_hook` used by the Gevent plugin to adhere to the new
  API.
2015-01-08 09:44:03 +02:00
Roberto De Ioris b59294db89 implemented uwsgi::opt in perl 2014-12-12 07:01:08 +01:00
Unbit f42294c0a3 fix #778 2014-11-21 08:36:14 +01:00
Roberto De Ioris 2b24f7b885 fixed destruction 2014-11-16 21:00:35 +01:00
Unbit 14702977a4 added active-workers signal target, aimed at improving #58 2014-11-16 18:48:12 +01:00
Ævar Arnfjörð Bjarmason e0e8bb1932 psgi: Don't free the wsgi_req->async_environ atexit, only in request context
The atexit hooks can be called outside of the request context. See the
discussion at https://github.com/unbit/uwsgi/pull/772
2014-11-11 16:03:00 +00:00
Ævar Arnfjörð Bjarmason 0a27a334cb psgi: Remove the check for the async mode in uwsgi_perl_atexit
According to Roberto this is just copy/pasted from the Python code, see
1.0.1-289-gd7e8523.
2014-11-11 16:00:49 +00:00
Ævar Arnfjörð Bjarmason 1dcdb72054 psgi: Move the destruction of the perl interpreter to the atexit hook + more bugfixes
My previous commit ensured that we called the DESTROY hook but we
wouldn't properly call the atexit hook. Now when you start uWSGI as
instructed in that commit you'll get this:

 * localhost:1234?0:

   $$: Calling DESTROY

 * localhost:1234?1:

   *** psgix.harakiri.commit requested ***
   ...The work of process 523 is done. Seeya!
   523: Calling the atexit hook
   523: Calling DESTROY

 * Ctrl+C (or stop):

   ^CSIGINT/SIGQUIT received...killing workers...
   696: Calling the atexit hook

Before this change we wouldn't call the atexit hook when
psgix.harakiri.commit was requested by calling localhost:1234?1.

To make this work I removed the "if busy do not run atexit hooks"
condition added in 1.4-rc2-246-g499202e. Of course uWSGI is going to
think the worker is "busy", it's busy being destroyed, that doesn't mean
we should skip running the atexit hooks.

We'll still skip them under the "if hijacked do not run atexit hooks"
condition added in that commit, and the "managing atexit in async mode
is a real pain" condition added in 1.0.1-289-gd7e8523. Maybe those are
further bugs that need to be solved, but I don't know how to test those
modes.

Now we'll also call PERL_SET_CONTEXT() and PERL_SYS_TERM() appropriately
during destruction. Note that the latter should only be called once even
if you have multiple interpreters.
2014-11-11 13:09:05 +00:00
Ævar Arnfjörð Bjarmason 7ef33950c5 psgi: Ensure that we call any DESTROY hooks on psgix.harakiri.commit
Before this we'd just exit(0) and let the OS clean up after us, but
e.g. with post-buffering=1 we'll end up with a temporary file in /tmp
that we won't clean up when we exit unless DESTROY is called.

This resulted in us leaking files in /tmp if we ever had a request where
the last request before a harakiri was a POST request with a body we'd
buffer to /tmp.

We'd have similar leaks in any user-defined code that required DESTROY
to run.

Aside from this I'm still not very comfortable with what this whole code
here in psgi_plugin.c and psgi_loader.c is doing when managing the
interpreter(s). It:

 * Doesn't consistently call PERL_SET_CONTEXT() as described in "perldoc
   perlembed".

 * Nothing calls PERL_SYS_TERM() either.

 * Should we be calling uwsgi_perl_free_stashes() here too?

To test this:

    UWSGI_PROFILE=psgi python uwsgiconfig.py --build
    ./uwsgi --master --http-socket localhost:1234 --psgi t/perl/test_harakiri.psgi

Then elsewhere:

    curl 'localhost:1234?0'
    curl 'localhost:1234?1'

Both of those should emit "Calling DESTROY".
2014-11-10 18:57:52 +00:00
Mattia Barbon c6f6171910 Fix latent refcounting bug
It can be reproduced by enabling the Perl debugger inside a PSGI application:

    {
        package DB;

        sub DB { }
        sub sub { &$sub }
    }

    $^P = 0x73f;

    sub { [200, ['Content-Type' => 'text/plain'], ['Hello World']] }

For every request the following warnings are emitted:

    Attempt to free unreferenced scalar: SV 0xfea6e8, Perl interpreter: 0xd534a0.
    Attempt to free unreferenced scalar: SV 0xfea718, Perl interpreter: 0xd534a0.

where the unreferenced scalars are the uwsgi::input/uwsgi::error instances
created in build_psgi_env.

The calling convention for Perl subroutines is that the values pushed on the
stack must be mortalized in the callee, and if the caller wants to retain them,
it must do a SvREFCNT_inc to undo the effect of the mortalization.

Before this patch XS_input/XS_error were not mortalizing the value, and
uwsgi_perl_obj_new was not incrementing the reference count, so the two bugs
balanced each other.

When running under debugger, Perl forwards all function/method calls to
DB::sub, which causes a mortal copy of the return value of
uwsgi::input/error::new to be pushed on the stack. The value is cleared by the
FREETMPS at the end of uwsgi_perl_obj_new, and the freed value is added to the
environment hash. The warning is emitted at the end of the request when the
environment hash is freed and Perl notices that some of the values has been
already freed.
2014-11-01 21:05:54 +01:00
Mattia Barbon cdf2a24aa5 Remove unnecessary mortalization
newRV(sv_newmortal()) is equivalent to newRV_noinc(newSV(0)): the former
creates a new SV with refcount 1, schedules a decrement "soon" (the
mortalization) and increments the refcount, the net result is a refcount of 1,
which is what the latter does.
2014-11-01 20:43:37 +01:00
Unbit ed2ca5d333 request buffer_size is now 64bit (except for uwsgi protocol) 2014-09-21 08:26:20 +02:00
Unbit 68bc194624 ensure PSGI response headers are in the right format [2] 2014-09-15 06:47:22 +02:00
Unbit 5bc5b7e01f ensure PSGI response headers are in the right format 2014-09-15 06:38:59 +02:00
Mike Kaplinskiy 1a4f333912 Allow --async 1 (i.e. sync mode, but with a different loop engine) 2014-08-17 13:13:17 -04:00
Unbit c6d90f3c42 fixed #669 2014-08-12 13:11:54 +02:00
Unbit daa1882921 improved harakiri for signal handlers 2014-06-23 13:42:55 +02:00
Unbit 2e14da983a first attempt in implementing per-core harakiri 2014-06-23 12:37:22 +02:00
Unbit 7b1138e5ef added perl low-level hook 2014-05-26 14:59:33 +02:00
Roberto De Ioris 9e618ee105 implemented (early) shared per interpreter 2014-05-12 03:05:33 +02:00
Roberto De Ioris 5ffd5f9bb9 preapre for psgi early-exec 2014-05-08 07:26:31 +02:00
Unbit 9d98b3b4ef better to mark it as 2.1, we can merge to master later 2014-05-05 15:58:36 +02:00
Unbit fa269b8838 start investigating argc/argv implications 2014-05-03 11:44:24 +02:00
Unbit 8b8b57769e first prototype of a fork server 2014-05-03 10:17:27 +02:00
Unbit 8f18c77587 fixed sharedarea waiting 2014-04-29 11:19:39 +02:00
Roberto De Ioris 6f671936fc added uwsgi::add_var to perl 2014-03-17 06:18:15 +01:00
Ævar Arnfjörð Bjarmason 13d8505738 perl: fix one-off error in 2.0-103-gf041d10 causing warnings in e.g. Plack::Request
This newly added support for read() offsets started causing "Use of
uninitialized value in subroutine entry" warnings.

This is all because there was a test for the number of items on the
stack, which ignored that the first argument is always the object, so 3
arguments to read() actually yields 4 arguments on the stack, not 3.

As a result we'd be calling SvIV() on a stack item that wasn't actually
passed in.
2014-03-15 21:05:18 +00:00
Ævar Arnfjörð Bjarmason af95f90fb0 perl: fix trivial spelling error in psgi_loader.c error message 2014-03-15 13:47:07 +00:00
Ævar Arnfjörð Bjarmason 97809192c8 perl: fix a regression with caller() not indicating the starting *.psgi program
In 2.0.1-41-g3480c30 I introduced a regression with how the top-level
stackframe would appear within Perl programs. Before we'd show the
filename of the *.psgi file, but after we just showed "-e".

We can retain the bugfix I added in 2.0.1-41-g3480c30 while having a
sensible stacktrace by overriding the file via the #line directive.
2014-03-15 13:10:30 +00:00
Unbit d3493df032 do not expode if Devel::StackTrace is missing 2014-02-26 17:06:21 +01:00
Unbit 757c76b569 fix -bash in perl hooks 2014-02-26 12:12:41 +01:00
Unbit 30eebfd335 better perl do usage and --perl-no-plack flag 2014-02-26 12:07:00 +01:00
Unbit c5d7d30439 try to better address #553 2014-02-26 11:39:00 +01:00
Ævar Arnfjörð Bjarmason 3480c30674 perl: Don't run BEGIN blocks twice in the provided *.psgi
The psgi loaded was calling perl_parse() with the script ostensibly to
set up xsinit.

However it would also call perl_parse() with the path to our *.psgi
file, whith the result that any BEGIN block in the *.psgi file would be
run twice, but anything outside BEGIN blocks would only run once.

This means that any code within explicit BEGIN blocks will run twice,
and any "use" statement in the *.psgi file will run its import() routine
twice, but due to the module being in %INC already we won't actually
compile things twice.

The previous behavior dates all the way back to the initial introduction
of the PSGI plugin in 299fd9c.

Then when support for local::lib was added in 7cbe751 we initially did a
perl_eval_pv() of a "use" statement like I'm doing here again now, but
later on in 1561dd3 changed it to call perl_parse with the commit
message "another PSGI loading fix".

Since there's no info on what that fixed or what was broken before I
have no idea if I'm introducing a regression here, but I don't see why
this way of loding local::lib shouldn't work, and it correctly munges
@INC for me when I try it.

We may still have this bug in the remaining perl_parse() calls that
remain for supporting "preinit" and "mule".

I haven't tested those modes (I don't use them), but when we load the
Perl apps we should only perl_parse() once with -e1, and then
perl_eval_pv() to actually load the application. We should not call
perl_parse() on code that we're just about to perl_eval_pv(), or we'll
run into this bug.

To test this just run:

    ./uwsgi --http 127.0.0.1:8080 --psgi ./t/perl/test.psgi

It'll no longer PANIC on the BEGIN block being run twice now, at least
in that simplistic non-"preinit" non-"mule" mode.
2014-02-25 18:07:42 +00:00
Unbit d878b65401 added first round of tests for psgi input with offset 2014-02-05 12:37:46 +01:00
Unbit ded149fc80 fixed perl stacktrace usage 2014-02-05 11:34:14 +01:00
Unbit f041d1095d perl refactoring 2014-02-05 11:21:01 +01:00
Roberto De Ioris 843a3b18ad fixed hv_delete on clang 2014-01-05 09:35:49 +01:00
Unbit ea19e1e67b perl spool arg must be a hashref 2014-01-05 09:01:17 +01:00