Skip to content

bug: syslog plugin crashes with nil-index error instead of returning an error when logger init fails #13969

Description

@bhuvan-somisetty

Current Behavior

In apisix/plugins/syslog/init.lua, send_syslog_data() fetches the logger instance from the lrucache and correctly checks whether it failed:

https://github.com/apache/apisix/blob/master/apisix/plugins/syslog/init.lua#L45-L78

local function send_syslog_data(conf, log_message, api_ctx)
    local err_msg
    local res = true

    core.log.info("sending a batch logs to ", conf.host, ":", conf.port)

    -- fetch it from lrucache
    local logger, err = core.lrucache.plugin_ctx(
        lrucache, api_ctx, nil, logger_socket.new, logger_socket, {
            host = conf.host,
            port = conf.port,
            flush_limit = conf.flush_limit,
            drop_limit = conf.drop_limit,
            timeout = conf.timeout,
            sock_type = conf.sock_type,
            pool_size = conf.pool_size,
            tls = conf.tls,
        }
    )

    if not logger then
        res = false
        err_msg = "failed when initiating the sys logger processor".. err
    end

    -- reuse the logger object
    local ok, err = logger:log(log_message)
    ...

When logger_socket.new(...) fails (e.g. bad host/port, socket/resource errors), logger is nil and the if not logger then branch correctly sets res = false and builds err_msg. But there is no return in that branch, so execution falls through to the very next line, logger:log(log_message), which indexes a nil value.

This throws an uncaught Lua runtime error ("attempt to index a nil value") inside the log-phase batch processor callback, instead of returning the already-built (false, err_msg) back to the caller like every other error branch in this codebase does (e.g. the same pattern in apisix/plugins/lago.lua's send_http_data, or the if not ok then branch a few lines below in this same function).

Expected Behavior

When the logger fails to initialize, send_syslog_data should return early with res, err_msg (i.e. false, err_msg), the same way the function already does for the logger:log() failure case just below it, instead of crashing on a nil index.

Suggested fix — add a return res, err_msg right after err_msg is set in the if not logger then block:

    if not logger then
        res = false
        err_msg = "failed when initiating the sys logger processor".. err
        return res, err_msg
    end

This is a small, single-file, single-function fix and should be a good first issue for a new contributor.

Error Logs

attempt to index a nil value (local 'logger')

(Uncaught Lua runtime error thrown from logger:log(log_message) in apisix/plugins/syslog/init.lua, surfaced in the APISIX error log during the log phase.)

Steps to Reproduce

  1. Run APISIX via the Docker image (or locally built).
  2. Create a Route with the Admin API and enable the syslog plugin on it, pointing host/port at a syslog endpoint.
  3. Force core.lrucache.plugin_ctx(...) / logger_socket.new(...) to fail for that route (e.g. point host/port at an address the socket cannot connect to, or otherwise make resty.logger.socket's new() return nil, err).
  4. Send a request through the route and observe an unhandled "attempt to index a nil value" error in the log phase instead of a clean, logged (false, err_msg) failure.

Environment

  • APISIX version (run apisix version): master branch (code unchanged for a long time; present in released versions too)
  • Operating system (run uname -a): not relevant — logic bug, reproducible on any OS
  • OpenResty / Nginx version (run openresty -V or nginx -V): not relevant — pure Lua control-flow bug
  • etcd version, if relevant: N/A
  • APISIX Dashboard version, if relevant: N/A
  • Plugin runner version, for issues related to plugin runners: N/A
  • LuaRocks version, for installation issues: N/A

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions