Skip to content

Commit df83c16

Browse files
authored
fix: stop from get loaded context in async (#3852)
1 parent 5f1ce1c commit df83c16

6 files changed

Lines changed: 28 additions & 34 deletions

File tree

web/server/api/endpoints/directories.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ async def write_directory(
2121
) -> models.Directory:
2222
"""Create or rename a directory."""
2323
if new_path:
24-
new_path = await validate_path(new_path, settings)
24+
new_path = validate_path(new_path, settings)
2525
replace_file(settings.project_path / path, settings.project_path / new_path)
2626
return models.Directory(name=os.path.basename(new_path), path=new_path)
2727

web/server/api/endpoints/files.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525

2626

2727
@router.get("", response_model=models.Directory)
28-
async def get_files(settings: Settings = Depends(get_settings)) -> models.Directory:
28+
def get_files(settings: Settings = Depends(get_settings)) -> models.Directory:
2929
"""Get all project files."""
30-
return await _get_directory(settings.project_path, settings)
30+
return _get_directory(settings.project_path, settings)
3131

3232

3333
@router.get("/{path:path}", response_model=models.File)
@@ -56,7 +56,7 @@ async def write_file(
5656
"""Create, update, or rename a file."""
5757
path_or_new_path = path
5858
if new_path:
59-
path_or_new_path = await validate_path(new_path, settings)
59+
path_or_new_path = validate_path(new_path, settings)
6060
replace_file(settings.project_path / path, settings.project_path / path_or_new_path)
6161
else:
6262
full_path = settings.project_path / path
@@ -70,7 +70,7 @@ async def write_file(
7070
format_file_status = models.FormatFileStatus(
7171
status=models.Status.INIT, path=path_or_new_path
7272
)
73-
path_to_model_mapping = await get_path_to_model_mapping(settings=settings)
73+
path_to_model_mapping = get_path_to_model_mapping(settings=settings)
7474
model = path_to_model_mapping.get(Path(full_path))
7575
default_dialect = config.dialect
7676
dialect = model.dialect if model and model.is_sql else default_dialect
@@ -118,8 +118,8 @@ async def delete_file(
118118
)
119119

120120

121-
async def _get_directory(path: str | Path, settings: Settings) -> models.Directory:
122-
context = await get_context(settings)
121+
def _get_directory(path: str | Path, settings: Settings) -> models.Directory:
122+
context = get_context(settings)
123123
ignore_patterns = context.config.ignore_patterns if context else c.IGNORE_PATTERNS
124124

125125
def walk_path(

web/server/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ async def dispatch() -> None:
3535

3636
app.state.dispatch_task.cancel()
3737
app.state.watch_task.cancel()
38-
context = await get_context_or_raise(settings=get_settings())
38+
context = get_context_or_raise(settings=get_settings())
3939
context.close()
4040

4141

web/server/settings.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from __future__ import annotations
22

3-
import asyncio
3+
import contextlib
44
import logging
55
import os
6+
import threading
67
import typing as t
7-
from contextlib import asynccontextmanager
88
from functools import lru_cache
99
from pathlib import Path
1010

@@ -19,7 +19,8 @@
1919
from web.server.models import Mode
2020

2121
logger = logging.getLogger(__name__)
22-
get_context_lock = asyncio.Lock()
22+
23+
get_loaded_context_lock = threading.Lock()
2324

2425
MODE_TO_MODULES = {
2526
models.Mode.IDE: {
@@ -82,46 +83,39 @@ def _get_path_to_model_mapping(context: Context) -> dict[Path, Model]:
8283
return {model._path: model for model in context._models.values()}
8384

8485

85-
async def get_path_to_model_mapping(
86+
def get_path_to_model_mapping(
8687
settings: Settings = Depends(get_settings),
8788
) -> dict[Path, Model]:
8889
try:
89-
async with asynccontextmanager(get_loaded_context)(settings) as context:
90+
with contextlib.contextmanager(get_loaded_context)(settings) as context:
9091
return _get_path_to_model_mapping(context)
9192
except Exception:
9293
logger.exception("Error creating a context")
9394
return {}
9495

9596

96-
async def get_loaded_context(settings: Settings = Depends(get_settings)) -> t.AsyncGenerator:
97-
loop = asyncio.get_running_loop()
98-
97+
def get_loaded_context(
98+
settings: Settings = Depends(get_settings),
99+
) -> t.Generator[Context, None]:
99100
try:
100-
async with get_context_lock:
101-
yield await loop.run_in_executor(
102-
None,
103-
_get_loaded_context,
104-
settings.project_path,
105-
settings.config,
106-
settings.gateway,
107-
)
101+
with get_loaded_context_lock:
102+
yield _get_loaded_context(settings.project_path, settings.config, settings.gateway)
108103
except Exception:
109104
raise ApiException(
110105
message="Unable to create a loaded context",
111106
origin="API -> settings -> get_loaded_context",
112107
)
113108

114109

115-
async def get_context(settings: Settings = Depends(get_settings)) -> t.Optional[Context]:
110+
def get_context(settings: Settings = Depends(get_settings)) -> t.Optional[Context]:
116111
try:
117-
async with get_context_lock:
118-
return _get_context(settings.project_path, settings.config, settings.gateway)
112+
return _get_context(settings.project_path, settings.config, settings.gateway)
119113
except Exception:
120114
return None
121115

122116

123-
async def get_context_or_raise(settings: Settings = Depends(get_settings)) -> Context:
124-
context = await get_context(settings)
117+
def get_context_or_raise(settings: Settings = Depends(get_settings)) -> Context:
118+
context = get_context(settings)
125119
if not context:
126120
raise ApiException(
127121
message="Unable to create a context",

web/server/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ def func_wrapper() -> R:
5151
return await loop.run_in_executor(None, func_wrapper)
5252

5353

54-
async def validate_path(path: str, settings: Settings = Depends(get_settings)) -> str:
55-
context = await get_context(settings)
54+
def validate_path(path: str, settings: Settings = Depends(get_settings)) -> str:
55+
context = get_context(settings)
5656
resolved_path = settings.project_path.resolve()
5757
full_path = (resolved_path / path).resolve()
5858
try:

web/server/watcher.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
async def watch_project() -> None:
2323
settings = get_settings()
24-
context = await get_context(settings)
24+
context = get_context(settings)
2525
paths = [
2626
(settings.project_path / c.AUDITS).resolve(),
2727
(settings.project_path / c.MACROS).resolve(),
@@ -57,10 +57,10 @@ async def watch_project() -> None:
5757
)
5858
)
5959
elif change == Change.added:
60-
directory = await _get_directory(path.parent, settings)
60+
directory = _get_directory(path.parent, settings)
6161
directories[directory.path] = directory
6262
elif path.is_dir() and change == Change.modified:
63-
directory = await _get_directory(path, settings)
63+
directory = _get_directory(path, settings)
6464
directories[directory.path] = directory
6565
elif path.is_file() and change == Change.modified:
6666
changes.append(

0 commit comments

Comments
 (0)