Skip to content

Replace lodash cloneDeep with efficient shallow copies to fix OOM crash#375

Closed
XboxBedrock with Copilot wants to merge 3 commits into
mainfrom
copilot/fix-bot-oom-crash
Closed

Replace lodash cloneDeep with efficient shallow copies to fix OOM crash#375
XboxBedrock with Copilot wants to merge 3 commits into
mainfrom
copilot/fix-bot-oom-crash

Conversation

Copilot AI commented Jan 9, 2026

Copy link
Copy Markdown

Bot crashes with heap exhaustion during startup due to excessive deep cloning in command registration. commandToSlash() runs for every command, each triggering multiple cloneDeep operations on large nested objects.

Changes

CommandUtils.util.ts - Replaced 9 cloneDeep calls:

  • Args arrays: cloneDeep(command.args)[...command.args]
  • Command objects: cloneDeep(command){ ...command, name: ... }
  • SlashCommandBuilder aliases: cloneDeep(builder)JSON.parse(JSON.stringify(builder))

replaceTypes.util.ts - Shallow copy modal components:

// Before: Deep clone entire modal structure
const jNew = _.cloneDeep(json)

// After: Shallow copy with explicit component array spreading
const jNew = {
    ...json,
    components: json.components.map(component => ({
        ...component,
        components: component.components ? [...component.components] : component.components
    }))
}

CommandMessage.ts - Build modal from template without cloning:

// Before: Clone template then mutate
const modal = _.cloneDeep(client.modals.getLocaleModal(modalName, locale))
modal.customId += "." + interaction.id

// After: Construct new object with spread
const modal = {
    ...modalTemplate,
    customId: modalTemplate.customId + "." + interaction.id,
    components: modalTemplate.components.map(...)
}

embed.command.ts - Removed redundant clone (.map() already creates new array)

Lodash imports removed from all modified files where no longer needed.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • mapbox-node-binary.s3.amazonaws.com
    • Triggering command: /usr/local/bin/node node /home/REDACTED/work/main-bot/main-bot/node_modules/.bin/node-pre-gyp install --fallback-to-build (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Bot OOM Crash</issue_title>
<issue_description>Describe the bug
Bot will consistently crash due to OOM


<--- Last few GCs --->
[1:0x7f58a3d3b3d0]  1086103 ms: Scavenge 4078.9 (4141.9) -> 4077.9 (4141.9) MB, 4.4 / 0.0 ms  (average mu = 0.171, current mu = 0.097) allocation failure 
[1:0x7f58a3d3b3d0]  1086109 ms: Scavenge 4079.4 (4142.4) -> 4078.4 (4142.4) MB, 4.1 / 0.0 ms  (average mu = 0.171, current mu = 0.097) allocation failure 
[1:0x7f58a3d3b3d0]  1088210 ms: Mark-sweep 4080.1 (4143.1) -> 4079.0 (4143.1) MB, 2098.4 / 0.0 ms  (average mu = 0.138, current mu = 0.053) allocation failure scavenge might not succeed
<--- JS stacktrace --->
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

Additional context
I’m fairly certain this is due to the way I abused lodash cloneDeep, I should probably fix that </issue_description>

<agent_instructions>Show me where lodash cloneDeep is being used excessively and remove it</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits January 9, 2026 00:16
Co-authored-by: XboxBedrock <68715625+XboxBedrock@users.noreply.github.com>
Co-authored-by: XboxBedrock <68715625+XboxBedrock@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix bot crash due to OOM issues Replace lodash cloneDeep with efficient shallow copies to fix OOM crash Jan 9, 2026
Copilot AI requested a review from XboxBedrock January 9, 2026 00:21
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.

Bot OOM Crash

2 participants