kong icon indicating copy to clipboard operation
kong copied to clipboard

Python Plugin Server crashes during execution

Open Smuggla opened this issue 1 year ago • 2 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Kong version ($ kong version)

3.5

Current Behavior

I are rewriting a lua plugin in python. This plugin writes incoming request data to a postgres instance.

#!/usr/bin/env python3
import kong_pdk.pdk.kong as kong
Schema = (
    {"pg_host": {"type": "string"}},
    {"pg_user": {"type": "string"}},
    {"pg_password": {"type": "string"}},
    {"pg_database": {"type": "string"}},
)
class Plugin(object):
    def __init__(self, config):
        self.config = config
    def log(self, kong: kong.kong):
        try:
            headers = kong.request.get_headers()
            sweet_header = headers['sweet_header']
            # no err in return, instead they are thrown if any
        except Exception as ex:
            kong.log("Could not retrieve headers, skipping request...")
        if not sweet_header:
            kong.log(" sweet_header not defined, skipping request...")
        else:
            kong.log("Logging data is defined, logging request...")
            url = headers['host'] + headers['uri']
            method = headers['method']
            upstream_status = kong.response.get_header['status']
            kong.log(sweet_header + " " + url + " " + method + " " + upstream_status )

Logs from proxy container:

2024/02/14 17:57:40 [notice] 1285#0: *736 [kong] mp_rpc.lua:155 [endpoint-analytics] Logging data is defined, logging request..., context: ngx.timer, client: <REDACTED>, server: 0.0.0.0:8443
2024/02/14 17:58:25 [error] 1285#0: *688 [kong] mp_rpc.lua:343 [endpoint-analytics] no data, context: ngx.timer, client: <REDACTED>, server: 0.0.0.0:8443
2024/02/14 17:58:25 [debug] 1285#0: *318 [lua] broker.lua:73: broadcast_events(): event published to 2 workers
2024/02/14 17:58:25 [debug] 1285#0: *171 [lua] callback.lua:114: do_event(): worker-events: handling event; source=plugin_server, event=reset_instances, wid=0
2024/02/14 17:58:25 [error] 1285#0: *171 [lua] callback.lua:98: do_handlerlist(): worker-events: event callback failed; source=plugin_server, event=reset_instances, wid=0 error='...local/share/lua/5.1/kong/runloop/plugin_servers/init.lua:273: attempt to index local 'current_instance' (a nil value)
stack traceback:
	...local/share/lua/5.1/kong/runloop/plugin_servers/init.lua:273: in function 'reset_instance'
	...local/share/lua/5.1/kong/runloop/plugin_servers/init.lua:393: in function <...local/share/lua/5.1/kong/runloop/plugin_servers/init.lua:392>
	[C]: in function 'xpcall'
	/usr/local/openresty/lualib/resty/events/callback.lua:83: in function 'do_handlerlist'
	/usr/local/openresty/lualib/resty/events/callback.lua:130: in function 'do_event'
	/usr/local/openresty/lualib/resty/events/worker.lua:69: in function 'do_event'
	/usr/local/openresty/lualib/resty/events/worker.lua:240: in function </usr/local/openresty/lualib/resty/events/worker.lua:222>', data={"conf":{"pg_host":"<REDACTED>","__plugin_id":"56180441-ba0d-44d5-b94c-bada843178c3","pg_user":"<REDACTED>","pg_password":"<REDACTED>","pg_database":"<REDACTED>","__key__":"plugins:endpoint-analytics:::::0d0c086d-61b6-419a-bed3-79ac1624354f","__seq__":23},"plugin_name":"endpoint-analytics"}, context: ngx.timer
2024/02/14 17:58:25 [debug] 1286#0: *5 [lua] callback.lua:114: do_event(): worker-events: handling event; source=plugin_server, event=reset_instances, wid=0

So the plugin server is working... but something is causing it to crash. Any ideas?

Expected Behavior

Continue with rest of plugin

Steps To Reproduce

No response

Anything else?

Normal Kong deployment with Helm, postgres backend, dev env

    resources:
      limits:
        cpu: 500m
        memory: 2G
      requests:
        cpu: 500m
        memory: 2G

Smuggla avatar Feb 15 '24 16:02 Smuggla

This is a fixed issue. Please update to 3.6.0.

StarlightIbuki avatar Feb 22 '24 04:02 StarlightIbuki

@StarlightIbuki , could you share the fix PR link here?

chronolaw avatar Mar 05 '24 03:03 chronolaw