1616#include "ngx_[% subsys %]_lua_probe.h"
1717
1818
19+ #define NGX_ [% SUBSYS %]_LUA_TIMER_ERRBUF_SIZE 128
20+
21+
1922typedef struct {
2023 void * * main_conf ;
2124 void * * srv_conf ;
@@ -565,29 +568,36 @@ ngx_[% subsys %]_lua_timer_handler(ngx_event_t *ev)
565568 lua_State * L ;
566569 ngx_int_t rc ;
567570 ngx_connection_t * c = NULL ;
568- [% req_type %] * r = NULL ;
569- ngx_ [% subsys %]_lua_ctx_t * ctx ;
570- ngx_ [% req_subsys %]_cleanup_t * cln ;
571571 ngx_pool_cleanup_t * pcln ;
572572
573+ [% req_type %] * r = NULL ;
574+ ngx_ [% req_subsys %]_cleanup_t * cln ;
575+ ngx_ [% subsys %]_lua_ctx_t * ctx ;
573576 ngx_ [% subsys %]_lua_timer_ctx_t tctx ;
574577 ngx_ [% subsys %]_lua_main_conf_t * lmcf ;
575578
576579[% IF http_subsys %]
577580 ngx_http_core_loc_conf_t * clcf ;
581+
578582[% ELSIF stream_subsys %]
579583 ngx_stream_core_srv_conf_t * clcf ;
580- [% END %]
581- [% IF stream_subsys %]
582584 ngx_stream_session_t * s ;
583585[% END %]
584586
587+ lua_Debug ar ;
588+ u_char * p ;
589+ u_char errbuf [NGX_ [% SUBSYS %]_LUA_TIMER_ERRBUF_SIZE ];
590+ const char * source ;
591+ const char * errmsg ;
592+
585593 ngx_log_debug0 (NGX_LOG_DEBUG_ [% SUBSYS %], ngx_cycle -> log , 0 ,
586594 "[% log_prefix %]lua ngx.timer expired" );
587595
588596 ngx_memcpy (& tctx , ev -> data , sizeof (ngx_ [% subsys %]_lua_timer_ctx_t ));
589597 ngx_free (ev );
590598
599+ ngx_ [% subsys %]_lua_assert (tctx .co_ref && tctx .co );
600+
591601 lmcf = tctx .lmcf ;
592602
593603 lmcf -> pending_timers -- ;
@@ -602,22 +612,22 @@ ngx_[% subsys %]_lua_timer_handler(ngx_event_t *ev)
602612 }
603613
604614 if (lmcf -> running_timers >= lmcf -> max_running_timers ) {
605- ngx_log_error ( NGX_LOG_ALERT , ngx_cycle -> log , 0 ,
615+ p = ngx_snprintf ( errbuf , NGX_ [% SUBSYS %] _LUA_TIMER_ERRBUF_SIZE - 1 ,
606616[% IF http_subsys %]
607- "%i lua_max_running_timers are not enough" ,
617+ "%i lua_max_running_timers are not enough" ,
608618
609619[% ELSIF stream_subsys %]
610- "stream lua: %i lua_max_running_timers are not enough" ,
620+ "stream lua: %i lua_max_running_timers are not enough" ,
611621[% END %]
612- lmcf -> max_running_timers );
622+ lmcf -> max_running_timers );
623+ * p = '\0' ;
624+ errmsg = (const char * ) errbuf ;
613625 goto failed ;
614626 }
615627
616628 c = ngx_ [% subsys %]_lua_create_fake_connection (tctx .pool );
617629 if (c == NULL ) {
618- ngx_log_error (NGX_LOG_ALERT , ngx_cycle -> log , 0 ,
619- "[% log_prefix %]failed to create fake connection to "
620- "run timer (co: %p)" , tctx .co );
630+ errmsg = "could not create fake connection" ;
621631 goto failed ;
622632 }
623633
@@ -630,18 +640,14 @@ ngx_[% subsys %]_lua_timer_handler(ngx_event_t *ev)
630640[% IF stream_subsys %]
631641 s = ngx_stream_lua_create_fake_session (c );
632642 if (s == NULL ) {
633- ngx_log_error (NGX_LOG_ALERT , ngx_cycle -> log , 0 ,
634- "stream lua failed to create fake session to run timer "
635- "(co: %p)" , tctx .co );
643+ errmsg = "could not create fake session" ;
636644 goto failed ;
637645 }
638646
639647[% ELSIF http_subsys %]
640648 r = ngx_http_lua_create_fake_request (c );
641649 if (r == NULL ) {
642- ngx_log_error (NGX_LOG_ALERT , ngx_cycle -> log , 0 ,
643- "failed to create fake request to run timer (co: %p)" ,
644- tctx .co );
650+ errmsg = "could not create fake request" ;
645651 goto failed ;
646652 }
647653[% END %]
@@ -683,9 +689,7 @@ ngx_[% subsys %]_lua_timer_handler(ngx_event_t *ev)
683689 ctx = ngx_stream_lua_create_ctx (s );
684690[% END %]
685691 if (ctx == NULL ) {
686- ngx_log_error (NGX_LOG_ALERT , ngx_cycle -> log , 0 ,
687- "[% log_prefix %]failed to create ctx to run timer "
688- "(co: %p)" , tctx .co );
692+ errmsg = "could not create ctx" ;
689693 goto failed ;
690694 }
691695
@@ -698,9 +702,7 @@ ngx_[% subsys %]_lua_timer_handler(ngx_event_t *ev)
698702
699703 pcln = ngx_pool_cleanup_add (r -> pool , 0 );
700704 if (pcln == NULL ) {
701- ngx_log_error (NGX_LOG_ALERT , ngx_cycle -> log , 0 ,
702- "[% log_prefix %]failed to add vm cleanup to run "
703- "timer (co: %p)" , tctx .co );
705+ errmsg = "could not add vm cleanup" ;
704706 goto failed ;
705707 }
706708
@@ -714,9 +716,7 @@ ngx_[% subsys %]_lua_timer_handler(ngx_event_t *ev)
714716
715717 cln = ngx_ [% req_subsys %]_cleanup_add (r , 0 );
716718 if (cln == NULL ) {
717- ngx_log_error (NGX_LOG_ALERT , ngx_cycle -> log , 0 ,
718- "[% log_prefix %]failed to add request cleanup to run "
719- "timer (co: %p)" , tctx .co );
719+ errmsg = "could not add request cleanup" ;
720720 goto failed ;
721721 }
722722
@@ -773,14 +773,26 @@ ngx_[% subsys %]_lua_timer_handler(ngx_event_t *ev)
773773
774774failed :
775775
776- if (tctx .co_ref && tctx .co ) {
777- lua_pushlightuserdata (tctx .co , ngx_ [% subsys %]_lua_lightudata_mask (
778- coroutines_key ));
779- lua_rawget (tctx .co , LUA_REGISTRYINDEX );
780- luaL_unref (tctx .co , -1 , tctx .co_ref );
781- lua_settop (tctx .co , 0 );
776+ /* co stack: func [args] */
777+ lua_getinfo (tctx .co , ">Sf" , & ar );
778+
779+ source = ar .source ;
780+
781+ if (source == NULL ) {
782+ source = "(unknown)" ;
782783 }
783784
785+ ngx_log_error (NGX_LOG_ALERT , ngx_cycle -> log , 0 ,
786+ "[% log_prefix %]lua failed to run timer with function "
787+ "defined at %s:%d: %s" ,
788+ source , ar .linedefined , errmsg );
789+
790+ lua_pushlightuserdata (tctx .co , ngx_ [% subsys %]_lua_lightudata_mask (
791+ coroutines_key ));
792+ lua_rawget (tctx .co , LUA_REGISTRYINDEX );
793+ luaL_unref (tctx .co , -1 , tctx .co_ref );
794+ lua_settop (tctx .co , 0 );
795+
784796 if (tctx .vm_state ) {
785797 ngx_ [% subsys %]_lua_cleanup_vm (tctx .vm_state );
786798 }
0 commit comments