-
Notifications
You must be signed in to change notification settings - Fork 270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memory leak: add rotatedPHP to kill and recreate PHP instances after a certain number of requests #990
Merged
Merged
Memory leak: add rotatedPHP to kill and recreate PHP instances after a certain number of requests #990
Changes from 12 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
c63b307
Memory leak: add rotatedPHP to kill and recreate PHP instances after …
adamziel ce75523
Formatting
adamziel 8916924
Only recreate the MEMFS nodes, not the entire filesystem
adamziel 67f54c8
Improve the documentation
adamziel c9810f2
Rotate PHP after 400 requests
adamziel 8f9fa2b
move the rotatedPHP call after the endpoint class declaration
adamziel 8792066
Test coverage
adamziel e9717d2
Implement the rotation via hotSwapPHPRuntime
adamziel f4cc574
Simplify runtime initialization in the worker
adamziel c5b9841
Fix unit tests and type issues
adamziel 3ae262b
Preserve PHP.ini path and custom SAPI name
adamziel 2f88b6c
Fix OPFS journaling
adamziel a0effff
Rely on events instead of monkey-patching the run method
adamziel b788563
Use PHP events instead of monkey-patching the `run` method
adamziel 9d90881
Test whether rotating the PHP runtime does indeed free the PHP memory
adamziel 41a5293
Adjust the test names and comments
adamziel 1a4e3a0
Document the choice of maxRequests=400
adamziel 67ceeb5
Improve comments
adamziel b766f31
Increase the timeout for the rotatePHP tests to 30 seconds
adamziel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import fs from 'fs'; | ||
import os from 'os'; | ||
import path from 'path'; | ||
import { NodePHP } from '..'; | ||
import { LatestSupportedPHPVersion, rotatedPHP } from '@php-wasm/universal'; | ||
|
||
const recreateRuntime = async (version: any = LatestSupportedPHPVersion) => | ||
await NodePHP.loadRuntime(version); | ||
|
||
async function rotate(php: any) { | ||
await php.run({ | ||
code: `<?php echo 'hi';`, | ||
}); | ||
await php.run({ | ||
code: `<?php echo 'hi';`, | ||
}); | ||
} | ||
|
||
describe('rotatedPHP()', () => { | ||
it('Should recreate the PHP instance after maxRequests', async () => { | ||
const recreateRuntimeSpy = vitest.fn(recreateRuntime); | ||
const php = await rotatedPHP({ | ||
php: new NodePHP(await recreateRuntimeSpy(), { | ||
documentRoot: '/test-root', | ||
}), | ||
recreateRuntime: recreateRuntimeSpy, | ||
maxRequests: 1, | ||
}); | ||
await rotate(php); | ||
expect(recreateRuntimeSpy).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('Should hotswap the PHP instance from 8.2 to 8.3', async () => { | ||
let nbCalls = 0; | ||
const recreateRuntimeSpy = vitest.fn(() => | ||
++nbCalls === 1 ? recreateRuntime('8.2') : recreateRuntime('8.3') | ||
); | ||
const php = await rotatedPHP({ | ||
php: new NodePHP(await recreateRuntimeSpy(), { | ||
documentRoot: '/test-root', | ||
}), | ||
recreateRuntime: recreateRuntimeSpy, | ||
maxRequests: 1, | ||
}); | ||
const version1 = ( | ||
await php.run({ | ||
code: `<?php echo PHP_VERSION;`, | ||
}) | ||
).text; | ||
const version2 = ( | ||
await php.run({ | ||
code: `<?php echo PHP_VERSION;`, | ||
}) | ||
).text; | ||
expect(version1).toMatch(/^8\.2/); | ||
expect(version2).toMatch(/^8\.3/); | ||
}); | ||
|
||
it('Should preserve the custom SAPI name', async () => { | ||
const php = await rotatedPHP({ | ||
php: new NodePHP(await recreateRuntime(), { | ||
documentRoot: '/test-root', | ||
}), | ||
recreateRuntime, | ||
maxRequests: 1, | ||
}); | ||
php.setSapiName('custom SAPI'); | ||
await rotate(php); | ||
const result = await php.run({ | ||
code: `<?php echo php_sapi_name();`, | ||
}); | ||
expect(result.text).toBe('custom SAPI'); | ||
}); | ||
|
||
it('Should preserve the MEMFS files', async () => { | ||
const php = await rotatedPHP({ | ||
php: new NodePHP(await recreateRuntime(), { | ||
documentRoot: '/test-root', | ||
}), | ||
recreateRuntime, | ||
maxRequests: 1, | ||
}); | ||
await rotate(php); | ||
php.mkdir('/test-root'); | ||
php.writeFile('/test-root/index.php', '<?php echo "hi";'); | ||
await rotate(php); | ||
expect(php.fileExists('/test-root/index.php')).toBe(true); | ||
expect(php.readFileAsText('/test-root/index.php')).toBe( | ||
'<?php echo "hi";' | ||
); | ||
}); | ||
|
||
it('Should not overwrite the NODEFS files', async () => { | ||
const php = await rotatedPHP({ | ||
php: new NodePHP(await recreateRuntime(), { | ||
documentRoot: '/test-root', | ||
}), | ||
recreateRuntime, | ||
maxRequests: 1, | ||
}); | ||
await rotate(php); | ||
php.mkdir('/test-root'); | ||
php.writeFile('/test-root/index.php', 'test'); | ||
php.mkdir('/test-root/nodefs'); | ||
|
||
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'temp-')); | ||
const tempFile = path.join(tempDir, 'file'); | ||
fs.writeFileSync(tempFile, 'playground'); | ||
const date = new Date(); | ||
date.setFullYear(date.getFullYear() - 1); | ||
fs.utimesSync(tempFile, date, date); | ||
try { | ||
php.mount(tempDir, '/test-root/nodefs'); | ||
|
||
await rotate(php); | ||
|
||
// Expect the file to still have the same utime | ||
const stats = fs.statSync(tempFile); | ||
expect(Math.round(stats.atimeMs)).toBe(Math.round(date.getTime())); | ||
|
||
// The MEMFS file should still be there | ||
expect(php.fileExists('/test-root/index.php')).toBe(true); | ||
} finally { | ||
fs.rmSync(tempFile); | ||
fs.rmdirSync(tempDir); | ||
} | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the point of these lines?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These
run()
calls are meant to trigger the internal PHP runtime rotation. I just replaced therotate()
function with something more readable:With no "echo" involved, the intention is hopefully less obscured