Skip to content

Commit 6b22037

Browse files
dulinrileymeta-codesync[bot]
authored andcommitted
Fix leftover configuration from test_config.py (#1971)
Summary: Pull Request resolved: #1971 Python tests on github failing with: ``` pyo3_runtime.PanicException: called `Result::unwrap()` on an `Err` value: Server(Listen(Tcp([::1]:0), Os { code: 99, kind: AddrNotAvailable, message: "Cannot assign requested address" })) ``` It is caused by the default transport for tests changing from unix -> tcp. It's not clear what caused this change, but probably a new test was added that changed the pytest-split groups. Make sure test_config.py resets to the default state when it is done. Reviewed By: rusch95, shayne-fletcher Differential Revision: D87664159 fbshipit-source-id: d8d1279d7909cf487df27500ceba56bc93e820c0
1 parent c7dae4c commit 6b22037

File tree

4 files changed

+81
-23
lines changed

4 files changed

+81
-23
lines changed

monarch_hyperactor/src/config.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ pub fn reload_config_from_env() -> PyResult<()> {
4747
Ok(())
4848
}
4949

50+
#[pyfunction()]
51+
pub fn reset_config_to_defaults() -> PyResult<()> {
52+
// Set all config values to defaults, ignoring even environment variables.
53+
hyperactor::config::global::reset_to_defaults();
54+
Ok(())
55+
}
56+
5057
/// Map from the kwarg name passed to `monarch.configure(...)` to the
5158
/// `Key<T>` associated with that kwarg. This contains all attribute
5259
/// keys whose `@meta(CONFIG = ConfigAttr { py_name: Some(...), .. })`
@@ -238,6 +245,13 @@ pub fn register_python_bindings(module: &Bound<'_, PyModule>) -> PyResult<()> {
238245
)?;
239246
module.add_function(reload)?;
240247

248+
let reset = wrap_pyfunction!(reset_config_to_defaults, module)?;
249+
reset.setattr(
250+
"__module__",
251+
"monarch._rust_bindings.monarch_hyperactor.config",
252+
)?;
253+
module.add_function(reset)?;
254+
241255
let configure = wrap_pyfunction!(configure, module)?;
242256
configure.setattr(
243257
"__module__",

python/monarch/_rust_bindings/monarch_hyperactor/config.pyi

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,26 @@ def reload_config_from_env() -> None:
2020
2121
This reads all HYPERACTOR_* environment variables and updates
2222
the global configuration.
23+
For any configuration setting not present in environment variables,
24+
this function will not change its value.
25+
"""
26+
...
27+
28+
def reset_config_to_defaults() -> None:
29+
"""Reset all configuration to default values, ignoring environment variables.
30+
Call reload_config_from_env() to reload the environment variables.
2331
"""
2432
...
2533

2634
def configure(
27-
default_transport: ChannelTransport = ChannelTransport.Unix,
28-
enable_log_forwarding: bool = False,
29-
enable_file_capture: bool = False,
30-
tail_log_lines: int = 0,
31-
) -> None: ...
35+
default_transport: ChannelTransport = ...,
36+
enable_log_forwarding: bool = ...,
37+
enable_file_capture: bool = ...,
38+
tail_log_lines: int = ...,
39+
**kwargs: object,
40+
) -> None:
41+
"""Change a configuration value in the global configuration. If called with
42+
no arguments, makes no changes. Does not reset any configuration"""
43+
...
44+
3245
def get_configuration() -> Dict[str, Any]: ...

python/tests/test_config.py

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,76 @@
66

77
# pyre-unsafe
88

9+
from contextlib import contextmanager
10+
911
import pytest
1012
from monarch._rust_bindings.monarch_hyperactor.channel import ChannelTransport
1113
from monarch._rust_bindings.monarch_hyperactor.config import (
1214
configure,
1315
get_configuration,
16+
reload_config_from_env,
17+
reset_config_to_defaults,
1418
)
1519

1620

21+
@contextmanager
22+
def configure_temporary(*args, **kwargs):
23+
"""Call configure, and then reset the configuration to the default values after
24+
exiting. Always use this when testing so that other tests are not affected by any
25+
changes made."""
26+
27+
try:
28+
configure(*args, **kwargs)
29+
yield
30+
finally:
31+
reset_config_to_defaults()
32+
# Re-apply any environment variables that were set for this test.
33+
reload_config_from_env()
34+
35+
1736
def test_get_set_transport() -> None:
1837
for transport in (
1938
ChannelTransport.Unix,
2039
ChannelTransport.TcpWithLocalhost,
2140
ChannelTransport.TcpWithHostname,
2241
ChannelTransport.MetaTlsWithHostname,
2342
):
24-
configure(default_transport=transport)
25-
assert get_configuration()["default_transport"] == transport
26-
# Succeed even if we don't specify the transport
27-
configure()
28-
assert (
29-
get_configuration()["default_transport"] == ChannelTransport.MetaTlsWithHostname
30-
)
43+
with configure_temporary(default_transport=transport):
44+
assert get_configuration()["default_transport"] == transport
45+
# Succeed even if we don't specify the transport, but does not change the
46+
# previous value.
47+
with configure_temporary():
48+
assert get_configuration()["default_transport"] == ChannelTransport.Unix
3149
with pytest.raises(TypeError):
32-
configure(default_transport="unix") # type: ignore
50+
with configure_temporary(default_transport="unix"): # type: ignore
51+
pass
3352
with pytest.raises(TypeError):
34-
configure(default_transport=42) # type: ignore
53+
with configure_temporary(default_transport=42): # type: ignore
54+
pass
3555
with pytest.raises(TypeError):
36-
configure(default_transport={}) # type: ignore
56+
with configure_temporary(default_transport={}): # type: ignore
57+
pass
3758

3859

3960
def test_nonexistent_config_key() -> None:
4061
with pytest.raises(ValueError):
41-
configure(does_not_exist=42) # type: ignore
62+
with configure_temporary(does_not_exist=42): # type: ignore
63+
pass
4264

4365

4466
def test_get_set_multiple() -> None:
45-
configure(default_transport=ChannelTransport.TcpWithLocalhost)
46-
configure(enable_log_forwarding=True, enable_file_capture=True, tail_log_lines=100)
67+
with configure_temporary(default_transport=ChannelTransport.TcpWithLocalhost):
68+
with configure_temporary(
69+
enable_log_forwarding=True, enable_file_capture=True, tail_log_lines=100
70+
):
71+
config = get_configuration()
72+
assert config["enable_log_forwarding"]
73+
assert config["enable_file_capture"]
74+
assert config["tail_log_lines"] == 100
75+
assert config["default_transport"] == ChannelTransport.TcpWithLocalhost
76+
# Make sure the previous values are restored.
4777
config = get_configuration()
48-
49-
assert config["enable_log_forwarding"]
50-
assert config["enable_file_capture"]
51-
assert config["tail_log_lines"] == 100
52-
assert config["default_transport"] == ChannelTransport.TcpWithLocalhost
78+
assert not config["enable_log_forwarding"]
79+
assert not config["enable_file_capture"]
80+
assert config["tail_log_lines"] == 0
81+
assert config["default_transport"] == ChannelTransport.Unix

scripts/common-setup.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ run_test_groups() {
179179
# sustainable/most robust solution.
180180
export CONDA_LIBSTDCPP="${CONDA_PREFIX}/lib/libstdc++.so.6"
181181
export LD_PRELOAD="${CONDA_LIBSTDCPP}${LD_PRELOAD:+:$LD_PRELOAD}"
182+
# Backtraces help with debugging remotely.
183+
export RUST_BACKTRACE=1
182184
local FAILED_GROUPS=()
183185
for GROUP in $(seq 1 10); do
184186
echo "Running test group $GROUP of 10..."

0 commit comments

Comments
 (0)