#1851·visdom

[Bug Report] Uncaught KeyError and AttributeError (HTTP 500) in SaveHandler on invalid /save payloads

Author: rishi919-rgbCreated Sep 22, 2026Updated Sep 22, 2026

Bug Description

While testing the environment persistence endpoints, I noticed that SaveHandler (/save) crashes with an unhandled exception (HTTP 500) when receiving malformed JSON, non-object JSON bodies, bodies without the "data" key, or non-string entries in "data".

In py/visdom/server/handlers/web_handlers.py:731-747:

python
class SaveHandler(BaseHandler):
    @staticmethod
    async def wrap_func(handler, args):
        envs = args["data"]
        envs = [escape_eid(eid) for eid in envs]
        ret = await save_envs_off_loop(handler, envs)
        handler.write(json.dumps(ret))

    @check_auth
    @check_readonly
    async def post(self):
        args = tornado.escape.json_decode(
            tornado.escape.to_basestring(self.request.body)
        )
        await self.wrap_func(self, args)
  1. Non-JSON body: tornado.escape.json_decode raises ValueError -> uncaught HTTP 500.
  2. Non-mapping body (e.g. [1, 2]): args["data"] raises TypeError -> uncaught HTTP 500.
  3. Missing "data" key (e.g. {}): args["data"] raises KeyError: 'data' -> uncaught HTTP 500.
  4. Non-list "data" (e.g. {"data": 123} or {"data": None}): raises TypeError -> uncaught HTTP 500.
  5. Non-string elements in "data" (e.g. {"data": [123]}): escape_eid raises AttributeError: 'int' object has no attribute 'strip' -> uncaught HTTP 500.

This was explicitly pinned in py/tests/integration/env_transfer.py:222-229:

python
def test_a_body_without_data_is_an_unhandled_error(self):
    """Documents today's contract: the missing key surfaces as a 500.

    Every sibling route validates its body and answers 400. ``/save`` reads
    ``args["data"]`` straight, so a body without it is a server error rather
    than a client one. Pinned so that fixing it is a visible change.
    """
    self.assertEqual(self.post_json("/save", {}).code, 500)

Sibling routes like /fork_env, /close, /upload_env, and /data validate their inputs and return HTTP 400 with a descriptive error reason.

Reproduction Steps

  1. Start the visdom server: python -m visdom.server
  2. Send POST request to /save with missing "data":
    bash
    curl -X POST http://localhost:8097/save -H "Content-Type: application/json" -d '{}'
  3. Server returns 500: Internal Server Error due to uncaught KeyError: 'data'.

Expected behavior SaveHandler should validate that the request body is valid JSON, an object, contains the required "data" key, and that "data" is a list of non-empty strings, returning HTTP 400 Bad Request with a helpful error reason instead of crashing with HTTP 500.