* Remove expired and empty sessions from the database. * * - Sessions with an `expires` date in the past are removed (expired). * - Sessions with no expiry that contain no data beyond the default cookie are removed. * These are the empty sessions that accumulate indefinitely (bug #5010
()
| 92 | * sessionstorage table (#7830) doesn't load every key into memory at once. |
| 93 | */ |
| 94 | async _cleanup() { |
| 95 | const now = Date.now(); |
| 96 | const startMs = Date.now(); |
| 97 | let removed = 0; |
| 98 | let scanned = 0; |
| 99 | let after: string | undefined; |
| 100 | let budgetExhausted = false; |
| 101 | // eslint-disable-next-line no-constant-condition |
| 102 | while (true) { |
| 103 | const page = await DB.findKeysPaged('sessionstorage:*', null, { |
| 104 | limit: CLEANUP_PAGE_SIZE, |
| 105 | ...(after != null ? {after} : {}), |
| 106 | }); |
| 107 | if (!page || page.length === 0) break; |
| 108 | // Defensive: a buggy backend that returns the cursor key would loop |
| 109 | // forever. `after` is exclusive, so the first key of the next page must |
| 110 | // be strictly greater than the previous cursor. Log so an operator can |
| 111 | // notice partial cleanup caused by a pagination regression. |
| 112 | if (after != null && page[0] <= after) { |
| 113 | logger.error( |
| 114 | `Session cleanup: paged cursor did not advance (after=${after}, ` + |
| 115 | `page[0]=${page[0]}); aborting this run to prevent an infinite loop`); |
| 116 | break; |
| 117 | } |
| 118 | for (const key of page) { |
| 119 | scanned++; |
| 120 | const sess = await DB.get(key); |
| 121 | if (!sess) { |
| 122 | await DB.remove(key); |
| 123 | removed++; |
| 124 | continue; |
| 125 | } |
| 126 | const expires = sess.cookie?.expires; |
| 127 | if (expires) { |
| 128 | if (new Date(expires).getTime() <= now) { |
| 129 | await DB.remove(key); |
| 130 | removed++; |
| 131 | } |
| 132 | } else { |
| 133 | const hasData = Object.keys(sess).some((k) => k !== 'cookie'); |
| 134 | if (!hasData) { |
| 135 | await DB.remove(key); |
| 136 | removed++; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | after = page[page.length - 1]; |
| 141 | if (Date.now() - startMs > CLEANUP_MAX_RUNTIME_MS) { |
| 142 | budgetExhausted = true; |
| 143 | break; |
| 144 | } |
| 145 | // Yield to the event loop between pages so request handlers can run and |
| 146 | // the DB driver can release the previous page's buffered rows. |
| 147 | await new Promise((resolve) => setImmediate(resolve)); |
| 148 | } |
| 149 | if (budgetExhausted) { |
| 150 | logger.warn( |
| 151 | `Session cleanup: hit ${CLEANUP_MAX_RUNTIME_MS}ms budget after scanning ` + |
no test coverage detected