-
-
Notifications
You must be signed in to change notification settings - Fork 16
[BUGFIX] Improve PlantumlRenderer robustness and temp file cleanup #1281
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
base: main
Are you sure you want to change the base?
[BUGFIX] Improve PlantumlRenderer robustness and temp file cleanup #1281
Conversation
packages/guides-graphs/tests/unit/Renderer/PlantumlRendererTest.php
Outdated
Show resolved
Hide resolved
- Handle mkdir race condition with triple-check pattern - Check tempnam() return value for false - Clean up temporary .pu and .svg files after rendering - Fix test cleanup to delete files before rmdir
86c31db to
f1ecc50
Compare
Replace dangerous glob-based cleanup with safe removeTempDirSafely(): - Validates directory is under system temp using realpath() - Validates directory has expected 'plantuml-test-' prefix - Uses RecursiveIteratorIterator for proper recursive deletion Addresses review feedback about potential catastrophic deletion if $tempDir variable were to be empty or corrupted.
f1ecc50 to
9258580
Compare
|
sorry for the noise, should be fine now. |
|
|
||
| if (!is_dir($this->tempDirectory)) { | ||
| mkdir($this->tempDirectory, 0o755, true); | ||
| if (!is_dir($this->tempDirectory) && !@mkdir($this->tempDirectory, 0o755, true) && !is_dir($this->tempDirectory)) { |
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.
I think it's better to split the conditions here. And not to perform operations in an condition like mkdir those are very hard to detect, people do not expect them in there.
Also the !is_dir($this->tempDirectory) is performed twice, that seems to be invalid.
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.
That's a simple trinary operation, not so uncommon/unknown, and the duplicated is_dir() is intentionally, that's the whole point of it.
if (!is_dir($this->tempDirectory)) {
// 2. Attempt Creation: Try to create it with 0755 permissions.
// The '@' suppresses a PHP warning if it fails.
// 'true' allows recursive creation of parent folders.
$creationSuccessful = @mkdir($this->tempDirectory, 0755, true);
if (!$creationSuccessful) {
// 3. Final Verification (Race Condition Check):
// If mkdir failed, it might be because another process
// created it between step 1 and step 2.
if (!is_dir($this->tempDirectory)) {
// IF IT STILL DOESN'T EXIST: Handle the real failure.
// This is where you would place your error logic or throw an exception.
throw new \Exception("Failed to create directory: " . $this->tempDirectory);
}
}
}
But this is already explained in the PR description.
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.
I get it's common to do this, but I do see it as a bad practice to combine things like this. It makes the code hard to understand.
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.
I get it's common to do this, but I do see it as a bad practice to combine things like this. It makes the code hard to understand.
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.
I already changed it. You may notice.
Addresses review feedback: the race-safe mkdir pattern is now in a dedicated method with clear documentation, making the intent more readable.
Summary
Follow-up to #1279 addressing code review findings for improved robustness.
Changes
1. Race-safe directory creation (lines 57, 112-130)
ensureDirectoryExists()helper method with clear documentation2. Check tempnam() return value (lines 66-74)
tempnam()can returnfalseon failure3. Clean up temporary files (lines 106-107)
.pusource file and.svgoutput after reading4. Safe test cleanup (test lines 47-53)
Files Changed
packages/guides-graphs/src/Graphs/Renderer/PlantumlRenderer.phppackages/guides-graphs/tests/unit/Renderer/PlantumlRendererTest.phpTest Plan