Corey Purcella
Corey Purcella

Reputation: 1

Node.js memcached throwing unhandled promise rejection from within try/catch

I'm using a promisified version of the node.js memcached library for caching.

    var get = promisify(memcached.get).bind(memcached);
    var set = promisify(memcached.set).bind(memcached);
    var add = promisify(memcached.add).bind(memcached);
    var del = promisify(memcached.del).bind(memcached);
    var incr = promisify(memcached.incr).bind(memcached);
    var getMulti = promisify(memcached.getMulti).bind(memcached);

These are exposed in the exports of my connections.js file. I figured that this would be sufficient, as I expected memcached to silently fail if it had any problems, and I could just fall back to the database.

Once or twice an hour (I would guess the application gets around 30,000 requests an hour) a request fails, and the output to the log is this:

(node:28788) UnhandledPromiseRejectionWarning: Error: Item is not stored
at Socket.notstored (c:\Workspaces\DOH\Vaccines\Service\node_modules\memcached\lib\memcached.js:445:20)
at Client.rawDataReceived (c:\Workspaces\DOH\Vaccines\Service\node_modules\memcached\lib\memcached.js:744:51)
at Client.BufferBuffer (c:\Workspaces\DOH\Vaccines\Service\node_modules\memcached\lib\memcached.js:678:12)
at Socket.bowlofcurry (c:\Workspaces\DOH\Vaccines\Service\node_modules\memcached\lib\utils.js:126:15)

This seems odd to me, but I figured I would just account for that possibility by wrapping my function in a try/catch.

        incr: async function (key, amount) {
            key = Buffer.from(key).toString('base64')
            try {
                if (amount == undefined) {
                    amount = 1
                }
                var value = await get(env + key);
                if (!value) {
                    await set(env + key, amount, 0)
                } else {
                    await incr(env + key, amount);
                }
                return get(env + key)
            } catch (e) {
                console.log("Memcached incr error: " + e);
                return 1
            }
        }

While I was debugging and connected to the memcached server, I saw the same error happen on a call to cache.incr(). The exception was not caught, it just failed the request and sent a 500 back to the client.

What am I missing? Does promisify not handle deep exceptions? The log is never able to trace the exception all the way back to a function call in my code; it just stops at utils.js in the memcached library. Why is that disconnect there?

Upvotes: 0

Views: 659

Answers (0)

Related Questions