Skip to content

Commit ac1b0b7

Browse files
committed
set_var
1 parent 45ce0ce commit ac1b0b7

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

lib/api_server/src/config.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
use bencher_config::{Config, BENCHER_CONFIG};
1+
use bencher_config::{BENCHER_CONFIG, Config};
22
use bencher_endpoint::{CorsResponse, Endpoint, Get, Put, ResponseAccepted, ResponseOk};
33
use bencher_json::{
4-
system::config::{JsonConsole, JsonUpdateConfig},
54
JsonConfig,
5+
system::config::{JsonConsole, JsonUpdateConfig},
66
};
77
use bencher_schema::{
8+
conn_lock,
89
context::ApiContext,
910
error::{bad_request_error, issue_error},
1011
model::user::{
1112
admin::AdminUser,
1213
auth::{AuthUser, BearerToken, PubBearerToken},
1314
},
1415
};
15-
use dropshot::{endpoint, HttpError, RequestContext, TypedBody};
16+
use dropshot::{HttpError, RequestContext, TypedBody, endpoint};
1617
use slog::Logger;
1718

1819
use super::restart::countdown;
@@ -91,7 +92,16 @@ async fn put_inner(
9192

9293
// TODO add validation here
9394
let config_str = serde_json::to_string(&config).map_err(bad_request_error)?;
94-
std::env::set_var(BENCHER_CONFIG, &config_str);
95+
{
96+
let _conn = conn_lock!(context);
97+
// SAFETY: This is safe because we are setting the environment variable
98+
// while holding a lock on the database connection.
99+
// This guarantees that no other thread is writing to the environment variable
100+
#[allow(unsafe_code, reason = "set environment variable")]
101+
unsafe {
102+
std::env::set_var(BENCHER_CONFIG, &config_str);
103+
}
104+
}
95105
Config::write(log, config_str.as_bytes())
96106
.await
97107
.map_err(|e| {

lib/bencher_config/src/config_tx.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,30 @@ use bencher_endpoint::Registrar;
44
#[cfg(feature = "plus")]
55
use bencher_json::system::config::{JsonLitestream, JsonPlus};
66
use bencher_json::{
7+
JsonConfig,
78
system::config::{
89
IfExists, JsonConsole, JsonDatabase, JsonLogging, JsonSecurity, JsonServer, JsonSmtp,
910
JsonTls, LogLevel, ServerLog,
1011
},
11-
JsonConfig,
1212
};
1313
use bencher_rbac::init_rbac;
1414
use bencher_schema::context::{ApiContext, Database, DbConnection};
1515
#[cfg(feature = "plus")]
1616
use bencher_schema::{context::RateLimiting, model::server::QueryServer};
1717
use bencher_token::TokenKey;
18+
use diesel::Connection;
1819
#[cfg(feature = "plus")]
1920
use diesel::connection::SimpleConnection;
20-
use diesel::Connection;
2121
use dropshot::{
2222
ApiDescription, ConfigDropshot, ConfigLogging, ConfigLoggingIfExists, ConfigLoggingLevel,
2323
ConfigTls, HttpServer,
2424
};
25-
use slog::{debug, error, info, Logger};
25+
use slog::{Logger, debug, error, info};
2626
use tokio::sync::mpsc::Sender;
2727

2828
use super::Config;
2929
#[cfg(feature = "plus")]
30-
use super::{plus::Plus, DEFAULT_BUSY_TIMEOUT};
30+
use super::{DEFAULT_BUSY_TIMEOUT, plus::Plus};
3131

3232
const DATABASE_URL: &str = "DATABASE_URL";
3333

@@ -273,7 +273,12 @@ fn diesel_database_url(log: &Logger, database_path: &str) {
273273
debug!(log, "Failed to find \"{DATABASE_URL}\"");
274274
}
275275
debug!(log, "Setting \"{DATABASE_URL}\" to {database_path}");
276-
std::env::set_var(DATABASE_URL, database_path);
276+
// SAFETY: This is safe because we are setting the environment variable
277+
// from a single thread at startup.
278+
#[allow(unsafe_code, reason = "set environment variable")]
279+
unsafe {
280+
std::env::set_var(DATABASE_URL, database_path);
281+
}
277282
}
278283

279284
#[cfg(feature = "plus")]

0 commit comments

Comments
 (0)