-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle out-of-memory errors in the worker thread (#4273)
- Loading branch information
1 parent
d6594e0
commit ffc53a9
Showing
27 changed files
with
224 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
import * as v8 from 'v8'; | ||
import * as os from 'os'; | ||
import fs from 'fs'; | ||
import { error, info, warn } from '@sonar/shared/helpers'; | ||
|
||
const MB = 1024 * 1024; | ||
|
||
export function logMemoryConfiguration() { | ||
const osMem = Math.floor(os.totalmem() / MB); | ||
const heapSize = getHeapSize(); | ||
const dockerMemLimit = readDockerMemoryLimit(); | ||
const dockerMem = dockerMemLimit ? `Docker mem: ${dockerMemLimit} MB. ` : ''; | ||
info(`OS memory ${osMem} MB. ${dockerMem}Node.js heap size limit: ${heapSize} MB.`); | ||
if (heapSize > osMem) { | ||
warn( | ||
`Node.js heap size limit ${heapSize} is higher than available memory ${osMem}. Check your configuration of sonar.javascript.node.maxspace`, | ||
); | ||
} | ||
} | ||
|
||
function readDockerMemoryLimit() { | ||
try { | ||
const mem = Number.parseInt(fs.readFileSync('/sys/fs/cgroup/memory.max', { encoding: 'utf8' })); | ||
if (Number.isInteger(mem)) { | ||
return mem; | ||
} | ||
} catch (e) { | ||
// probably not a docker env | ||
} | ||
return undefined; | ||
} | ||
|
||
function getHeapSize() { | ||
return Math.floor(v8.getHeapStatistics().heap_size_limit / MB); | ||
} | ||
|
||
export function logMemoryError(err: any) { | ||
switch (err?.code) { | ||
case 'ERR_WORKER_OUT_OF_MEMORY': | ||
error( | ||
`The analysis will stop due to the Node.js process running out of memory (heap size limit ${getHeapSize()} MB)`, | ||
); | ||
error( | ||
`You can see how Node.js heap usage evolves during analysis with "sonar.javascript.node.debugMemory=true"`, | ||
); | ||
error( | ||
'Try setting "sonar.javascript.node.maxspace" to a higher value to increase Node.js heap size limit', | ||
); | ||
error( | ||
'If the problem persists, please report the issue at https://community.sonarsource.com', | ||
); | ||
break; | ||
default: | ||
error(`The analysis will stop due to an unexpected error: ${err}`); | ||
error('Please report the issue at https://community.sonarsource.com'); | ||
break; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
import { logMemoryError } from '../src/memory'; | ||
|
||
describe('logMemoryError', () => { | ||
it('should log out-of-memory troubleshooting guide', () => { | ||
console.error = jest.fn(); | ||
|
||
logMemoryError({ code: 'ERR_WORKER_OUT_OF_MEMORY' }); | ||
|
||
expect(console.error).toHaveBeenCalledWith( | ||
expect.stringContaining( | ||
`The analysis will stop due to the Node\.js process running out of memory`, | ||
), | ||
); | ||
expect(console.error).toHaveBeenCalledWith( | ||
'You can see how Node.js heap usage evolves during analysis with "sonar.javascript.node.debugMemory=true"', | ||
); | ||
expect(console.error).toHaveBeenCalledWith( | ||
'Try setting "sonar.javascript.node.maxspace" to a higher value to increase Node.js heap size limit', | ||
); | ||
expect(console.error).toHaveBeenCalledWith( | ||
'If the problem persists, please report the issue at https://community.sonarsource.com', | ||
); | ||
}); | ||
|
||
it('should log default troubleshooting guide', () => { | ||
console.error = jest.fn(); | ||
|
||
logMemoryError('something failed'); | ||
|
||
expect(console.error).toHaveBeenCalledWith( | ||
'The analysis will stop due to an unexpected error: something failed', | ||
); | ||
expect(console.error).toHaveBeenCalledWith( | ||
'Please report the issue at https://community.sonarsource.com', | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.