json-server icon indicating copy to clipboard operation
json-server copied to clipboard

Database reloading unnecessarily when watching for file changes

Open mirorauhala opened this issue 3 years ago • 0 comments
trafficstars

Ordering the server to watch for changes in the database file will sometimes cause the server to reload the database after a PATCH request. The server will be offline during reloading for a short period of time and cause subsequent requests to fail.

This issue does not happen when -w flag is left out. Also at the end there's a memory leak warning. For what it's worth, the memory leak warning doesn't happen on every database reload, nor has it occurred when the -w flag is left out.

Command used: NODE_OPTIONS="--trace-warnings" npx json-server -H localhost -p 3010 -w ./db.json

I added the NODE_OPTIONS env to the command above to get the stacktrace for the memory leak.

Also during testing I closed my editor so that there's no change any "autosave" feature would trigger the reloading.

PATCH /tasks/15 200 2.377 ms - 63
  ./db.json has changed, reloading...

  Loading ./db.json
  ./db.json has changed, reloading...
PATCH /tasks/9 - - ms - -
  Done

  Resources
  http://localhost:3010/lists
  http://localhost:3010/tasks

  Home
  http://localhost:3010

  ./db.json has changed, reloading...

  Loading ./db.json
  ./db.json has changed, reloading...
  ./db.json has changed, reloading...
  Done

  Resources
  http://localhost:3010/lists
  http://localhost:3010/tasks

  Home
  http://localhost:3010

(node:33803) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 uncaughtException listeners added to [process]. Use emitter.setMaxListeners() to increase limit
    at _addListener (node:events:465:17)
    at process.addListener (node:events:487:10)
    at /Users/miro/.npm/_npx/aff12940958e4bf4/node_modules/json-server/lib/cli/run.js:141:15

Environment: Device: Mac Mini M1 OS: macOS Monterey 12.0.1 Node: v16.13.0 NPM: 8.1.0

mirorauhala avatar Dec 10 '21 13:12 mirorauhala