Skip to content

Commit

Permalink
[Core] Fix memory leak due to repetitive registration of FastAPI rout…
Browse files Browse the repository at this point in the history
…es (#1141)

# Description

What - Fix memory leak due to repetitive registration of FastAPI routes

Why - Constant memory leak when registering FastAPI routes

How - Run app setup just once

## Type of change

Please leave one option from the following and delete the rest:

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] New Integration (non-breaking change which adds a new integration)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] Non-breaking change (fix of existing functionality that will not
change current behavior)
- [ ] Documentation (added/updated documentation)

<h4> All tests should be run against the port production
environment(using a testing org). </h4>

### Core testing checklist

- [x] Integration able to create all default resources from scratch
- [x] Resync finishes successfully
- [x] Resync able to create entities
- [x] Resync able to update entities
- [x] Resync able to detect and delete entities
- [x] Scheduled resync able to abort existing resync and start a new one
- [x] Tested with at least 2 integrations from scratch
- [x] Tested with Kafka and Polling event listeners
- [x] Tested deletion of entities that don't pass the selector
  • Loading branch information
talsabagport authored Nov 12, 2024
1 parent 6aaaa28 commit c442cc4
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

<!-- towncrier release notes start -->

## 0.13.1 (2024-11-12)


### Bug Fixes

- Fix memory leak due to repetitive registration of FastAPI routes


## 0.13.0 (2024-11-10)


Expand Down
4 changes: 3 additions & 1 deletion integrations/fake-integration/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ async def resync_persons(kind: str) -> List[Dict[Any, Any]]:
return persons


initialize_fake_routes()


@ocean.on_start()
async def on_start() -> None:
print("Starting fake integration!")
initialize_fake_routes()
2 changes: 1 addition & 1 deletion integrations/fake-integration/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "fake-integration"
version = "0.1.14-dev"
version = "0.1.15-dev"
description = "A useless fake integration that helps us test the Ocean Core"
authors = ["Erik Zaadi <[email protected]>"]

Expand Down
10 changes: 9 additions & 1 deletion port_ocean/ocean.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ def __init__(
self.port_client, self.config.scheduled_resync_interval
)

self.app_initialized = False

def is_saas(self) -> bool:
return self.config.runtime == Runtime.Saas

Expand Down Expand Up @@ -112,7 +114,7 @@ async def execute_resync_all() -> None:
)
await repeated_function()

async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
def initialize_app(self) -> None:
self.fast_api_app.include_router(self.integration_router, prefix="/integration")

@asynccontextmanager
Expand All @@ -129,4 +131,10 @@ async def lifecycle(_: FastAPI) -> AsyncIterator[None]:
signal_handler.exit()

self.fast_api_app.router.lifespan_context = lifecycle
self.app_initialized = True

async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
if not self.app_initialized:
self.initialize_app()

await self.fast_api_app(scope, receive, send)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "port-ocean"
version = "0.13.0"
version = "0.13.1"
description = "Port Ocean is a CLI tool for managing your Port projects."
readme = "README.md"
homepage = "https://app.getport.io"
Expand Down

0 comments on commit c442cc4

Please sign in to comment.