Skip to content

Commit 3e1a26d

Browse files
jyn514syphar
authored andcommitted
Cleanup temporary directories at startup
1 parent e0a5fef commit 3e1a26d

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ memmap2 = "0.5.0"
8282
# iron dependencies
8383
iron = "0.6"
8484
router = "0.6"
85+
86+
# NOTE: if you change this, also double-check that the comment in `queue_builder::remove_tempdirs` is still accurate.
8587
tempfile = "3.1.0"
8688

8789
# Templating

src/docbuilder/rustwide_builder.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::error::Result;
88
use crate::index::api::ReleaseData;
99
use crate::repositories::RepositoryStatsUpdater;
1010
use crate::storage::{rustdoc_archive_path, source_archive_path};
11-
use crate::utils::{copy_dir_all, parse_rustc_version, CargoMetadata};
11+
use crate::utils::{copy_dir_all, parse_rustc_version, queue_builder, CargoMetadata};
1212
use crate::{db::blacklist::is_blacklisted, utils::MetadataPackage};
1313
use crate::{Config, Context, Index, Metrics, Storage};
1414
use anyhow::{anyhow, bail, Error};
@@ -314,7 +314,9 @@ impl RustwideBuilder {
314314
};
315315
krate.fetch(&self.workspace).map_err(FailureError::compat)?;
316316

317-
let local_storage = tempfile::Builder::new().prefix("docsrs-docs").tempdir()?;
317+
let local_storage = tempfile::Builder::new()
318+
.prefix(queue_builder::TEMPDIR_PREFIX)
319+
.tempdir()?;
318320

319321
let successful = build_dir
320322
.build(&self.toolchain, &krate, self.prepare_sandbox(&limits))

src/utils/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ mod copy;
1818
pub(crate) mod daemon;
1919
mod html;
2020
mod queue;
21-
mod queue_builder;
21+
pub(crate) mod queue_builder;
2222
mod rustc_version;
2323
pub(crate) mod sized_buffer;
2424

src/utils/queue_builder.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ use anyhow::Error;
33
use log::{debug, error, info, warn};
44
use std::panic::{catch_unwind, AssertUnwindSafe};
55
use std::sync::Arc;
6-
use std::thread;
76
use std::time::Duration;
7+
use std::{fs, io, thread};
8+
9+
pub(crate) const TEMPDIR_PREFIX: &str = "docsrs-docs";
810

911
// TODO: change to `fn() -> Result<!, Error>` when never _finally_ stabilizes
1012
pub fn queue_builder(
@@ -26,6 +28,10 @@ pub fn queue_builder(
2628
let mut status = BuilderState::Fresh;
2729

2830
loop {
31+
if let Err(e) = remove_tempdirs() {
32+
report_error(&anyhow::anyhow!(e).context("failed to remove temporary directories"));
33+
}
34+
2935
if !matches!(status, BuilderState::QueueInProgress) {
3036
thread::sleep(Duration::from_secs(60));
3137
}
@@ -70,3 +76,21 @@ pub fn queue_builder(
7076
}
7177
}
7278
}
79+
80+
/// Sometimes, when the server hits a hard crash or a build thread panics,
81+
/// rustwide_builder won't actually remove the temporary directories it creates.
82+
/// Remove them now to avoid running out of disk space.
83+
fn remove_tempdirs() -> Result<(), io::Error> {
84+
// NOTE: hardcodes that `tempfile::tempdir()` uses `std::env::temp_dir`.
85+
for entry in std::fs::read_dir(std::env::temp_dir())? {
86+
let entry = entry?;
87+
if !entry.path().starts_with(TEMPDIR_PREFIX) {
88+
continue;
89+
}
90+
if entry.metadata()?.is_dir() {
91+
fs::remove_dir_all(entry.path())?;
92+
}
93+
}
94+
95+
Ok(())
96+
}

0 commit comments

Comments
 (0)