Skip to content

[fix][core] Crash fixes for 24.05: internal event paths, unhandled database failures, batched events - #8295

Open
ar2rsawseen wants to merge 16 commits into
release.24.05from
fix/stats-crashes-24.05
Open

ar2rsawseen wants to merge 16 commits into
release.24.05from
fix/stats-crashes-24.05

Conversation

@ar2rsawseen

@ar2rsawseen ar2rsawseen commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

What and why

Crashes that 24.05 servers report to our crash tracking, fixed on release.24.05 (ports of master fixes plus new ones):

  • Internal plugin events requested as URL paths: cherry-picks of [fix][core] Refuse internal plugin events requested as URL paths #8293 (refuse paths outside /i and /o and internal events named under them).
  • Database failures nobody handles (plugins/pluginManager.js): unawaited wrapped collection operations log a failure instead of raising an unhandled rejection (24.05 has no db-level wrapper, so only collections are covered); the plugin directory scan logs an EMFILE.
  • "Callback was already called" / mergeQuery indexOf: same fix as master (ReadBatcher callback runs once; mergeQuery wraps object/array/null values).
  • Ports from master: reports subscription links answer 400/404 instead of reading user of undefined (cherry-pick of 0ec585e); render.js page errors without a message; an empty URL path; the unhandled _save of a failed job ("No such job in db"); star-rating /o/feedback/multiple-widgets-by-id answers 400 when widgets is not an array.
  • EPIPE on a closed stdout/stderr at shutdown is dropped.

Tests

  • New unit tests: plugins.pluginManager.unawaited-operations.js, api.parts.data.batched-writes-and-reads.js (fail without the fix, pass with it); mocha test/unit-tests/api.*.js on Node 20 passing except the network-bound countly-request timeouts.

  • The cherry-picked API tests (test/2.api/25.internal.event.paths.js, reports, star-rating) need the integration stack; CI runs them.

  • eslint (--rulesdir bin/eslint-rules) clean on changed files.

  • CMS cache clear (/i/cms/clear): the route called validateUserForWrite(clearCache, params) with its arguments swapped, and that validator does not correct them, so the route never answered. The cache entry id is now matched as a literal prefix. API test: test/3.api.write/16.cms.cache.clear.js.

  • Telemetry collection and dashboard start: the tracker's server-data collection and the dashboard's startup promise had no rejection handler; a database failure in either is now logged.

  • **Port of master'"s

  • Reports preview and PDF treat a report without report_type as core, as the generator already does.

  • views delete_view without view_id answers 400 (cherry-pick of master's 6726291, with its test).

🤖 Generated with Claude Code

ar2rsawseen and others added 9 commits November 25, 2026 23:04
/unsubscribe_report and /subscribe_report now decode the link before
touching the database. A link whose data is missing, is not JSON or does
not decrypt gets a 400 page; a link for a report that no longer exists
gets a 404 page. The page template has an error variant without the
resubscribe button, and render() no longer needs a report to answer.

Valid links behave as before, including the audit log entries.

(cherry picked from commit 0ec585e)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 0a347be59dabdd356be8877969606b000f0bc2a3)
…d a failed job save

- render: a headless chrome pageerror without an error object logged
  error.message and threw inside the event handler.
- requestProcessor: a request url without a pathname (url.parse returns
  null) threw on split before the request was answered.
- jobs manager: marking a failed job as done saved it without a handler,
  so a job removed from the database meanwhile ("No such job in db")
  became an unhandled rejection. The failure is logged.

Same fixes as master.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit ad36f6203475f257af6af17523b79a1029222880)
/o/feedback/multiple-widgets-by-id parsed the widgets parameter and called
map on the result. A value that parses to something other than an array
(widgets=null) threw a TypeError, an unhandled rejection with no answer
to the request. It now answers 400 "Invalid widgets", as on master.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 226a3d204f8ff8ac78717d7449491f7686767317)
requestProcessor dispatches unknown request paths to plugins by name. Plugin
events that core or plugins fire themselves, with their own data, could be
requested as URL paths (no credentials needed): /i/device_id, /i/user_merge,
/o/method/total_users, /i/event/delete, /i/events and /o/validate under /i and
/o, and every event outside them (/master, /sdk/user_properties,
/session/begin, /systemlogs, ...) where the API port is reachable directly.
Their hooks ran without that data, threw or returned a value, the dispatch
counted as handled, and the request was never answered, each hook logging an
unhandled rejection.

All 18 request-path dispatches in requestProcessor now go through
dispatchRequestPath, which refuses names outside /i and /o (request routes
live only there) and the internal events listed in INTERNAL_EVENTS, so these
requests answer 400 Invalid path without running any hook. Core's own
dispatches of these events are unchanged, and no plugin changes.

Adds test/2.api/25.internal.event.paths.js: each listed path and
/sdk/user_properties, bare and with request parameters, answers 400 Invalid
path, and the locale plugin's /o/langmap route still answers through the same
fall-through dispatch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 23ad626)
The /i/events route answered unknown sub-paths with "must be one of /all or
/me", copied from another route. It now names its own sub-paths:
change_visibility, delete_events, edit_map and whitelist_segments.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 3a9d0b4)
The tests request through the proxy, which sends only /i and /o to the API, so
/sdk/user_properties is answered 404 by the frontend before it reaches the
API (core CI: expected 400, got 404). requestProcessor's refusal of names
outside /i and /o protects API ports exposed without the proxy; it is checked
by a local run against the API port, not by these tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit d865ec9)
…nhandled rejections

- db wrapper: an operation called without a callback returns the driver's promise.
  When the caller neither awaits nor chains it (fire-and-forget), any
  MongoDB failure (pool cleared, NOT_PRIMARY, interrupted at shutdown,
  a bulkWrite or insertOne error) became an unhandled rejection. The
  wrapper now returns a promise that notices whether anybody subscribed
  to it: a caller that awaits or chains still gets the rejection, and an
  unsubscribed failure is logged through db:write.
- loadConfigs: the data masking configuration is loaded without waiting
  for it; a failed load (cursor not found, not primary) is now logged.
- loadConfigs and installMissingPlugins scan the plugin directory inside
  a database callback, where a failed scan (EMFILE) became an unhandled
  rejection. The failure is logged and the scan result left empty.

Adds a unit test for the unawaited operations.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit aa0d55b90ea54a7148bb4c61a2f59b0b797e11a5)
When the reader of the API process's stdout or stderr goes away (a
supervisor or log pipe closed, typically while shutting down), console
ignores the failed write but the EPIPE arrives later as an 'error' event
with no listener, an uncaught exception. Nothing can be written there any
more, so the API drops that one error; any other stream error is thrown
as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 57e349010864ccbc207399c01f79fad3b06b1e56)
…d call a batched read's callback once

- common.mergeQuery: an $addToSet / $push value queued as a single object,
  array or null (not {$each: [...]}) is wrapped before merging; it read $each
  of that value and threw inside event processing. A field only the later
  update sets is merged without an empty value.
- ReadBatcher callbacks: a callback that throws is no longer called again with
  its own error. That second call made async report "Callback was already
  called" as an unhandled rejection on every later event batch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 25, 2026 21:08
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-04T14:13:12.301269Z 4676278 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

ar2rsawseen and others added 3 commits November 26, 2026 11:11
… first

/i/cms/clear called validateUserForWrite(clearCache, params) with its
arguments swapped. Unlike validateUser, that validator does not swap them
back, so the route never answered. The entry id it clears by is now matched
as a literal prefix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Same fixes as master's #8246, for crashes 24.05 servers have the same code for:
- events: a segment list stored as something other than an array
- ping and server-stats jobs log errors and finish once
- job manager: a failure computing the next run, clearing a job
- dashboards: retry while the job class is not loaded yet
- plugin dispatch: log failing handlers nobody waits for
- push: missing app user, closed connection pools, missing push config,
  legacy FCM credentials
- reports and star-rating reports answer once
- system-utility: missing sub-path
- views: an unparseable period answers 400

Conflicts kept 24.05's versions where it already had a fix (#8245 views
period fallback, star-rating period check, request docs); the push
connector keeps 24.05's id handling with master's guard.

(cherry picked from commit 4a0868e)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…start

The telemetry collection the tracker runs 20 seconds after start and the
dashboard's startup promise had no rejection handler, so a database
failure in either became an unhandled rejection. Both are logged now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 04a939b6de

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

break;
case 'clear':
validateUserForWrite(countlyApi.mgmt.cms.clearCache, params);
validateUserForWrite(params, countlyApi.mgmt.cms.clearCache);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Require global-admin rights before clearing the shared CMS cache

With the arguments corrected, an administrator of any single app can now call /i/cms/clear with that app's app_id and delete the server-wide CMS cache. validateUserForWrite only checks administrative access to the supplied app (api/utils/rights.js), whereas cmsApi.clearCache deletes from the shared cms_cache collection without any app filter, using {} when _id is omitted. This newly exposes a global destructive operation to app administrators and app-scoped credentials, despite the adjacent CMS write route requiring global-admin rights. Use the global-admin validator here as well.

Useful? React with 👍 / 👎.

ar2rsawseen and others added 4 commits December 4, 2026 16:05
…wing

The preview and PDF routes treated a report with no report_type as a
plugin report and rendered its message's template, which a core report
does not have. The generator already defaults it to core; these routes
do the same now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
delete_view split view_id before checking it was sent, so a request
without it threw inside the handler and was never answered.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
/i/views?method=delete_view split qstring.view_id before validating the
request, so a call without view_id threw inside the dispatched promise:
the request was never answered and the unhandled rejection was reported
as a crash ("Cannot read properties of undefined (reading 'split')").

The split now runs after validateDelete, behind a view_id check that
answers 400 "Missing request parameter: view_id", and the value is cast
to a string before splitting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 6726291)

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4676278326

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread plugins/reports/tests.js
Comment on lines +1035 to +1036
const API_KEY_ADMIN = testUtils.get("API_KEY_ADMIN");
const APP_ID = testUtils.get("APP_ID");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Read subscription-test credentials after API setup runs

In the default Grunt test run without preconfigured COUNTLY_TEST_* environment variables, these constants capture empty strings during Mocha's suite-registration phase. test/4.plugins/1.plugin.tests.js loads this file before the tests in test/2.api/02.read.user.js and test/2.api/08.create.app.js populate the credentials. Consequently, the new before hook calls /i/reports/create?api_key=&app_id=, fails its expected-200 assertion, and prevents the subscription tests from running. Read both values inside the before hook or createReport instead.

Useful? React with 👍 / 👎.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants