Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory limit #303

Open
ra1nbow1 opened this issue Jan 23, 2024 · 5 comments
Open

Memory limit #303

ra1nbow1 opened this issue Jan 23, 2024 · 5 comments
Labels

Comments

@ra1nbow1
Copy link

The Question:
Are there any ways to set memory or time limit to prevent using infinite loops? I'm making and online compiler

@Almenon
Copy link
Collaborator

Almenon commented Jan 24, 2024

Memory limit - there's no function in the python-shell library to set this
time limit - there's no function in the python-shell library to set this. However, in Nodejs you can call setTimeout to call a function after a certain period of time. So you could stop your python process after a certain period of time.

https://nodejs.org/api/timers.html#settimeoutcallback-delay-args

There's multiple ways of limiting a process memory and CPU usage. One way is with containers. That's what repl.it does:

Great questions and we intend to write about this more in the future. We had to build our own container orchestration mostly for speed and customizability.
A bit of context: for every language/environment we have a Dockerfile (naturally) and a JSON configuration that describe how it runs, how it install packages, how it runs unit tests, how it formats code, etc. When we build the container we insert a program that we call pid1, it's the container's interface to the rest of the world.

The container manager creates pools of these containers with some rudimentary predictive logic to make sure we have enough containers to deliver on our promise of "loads in 2 seconds". When we take a container out of the pool, if we're reviving a container, we mount a GCS-backed fuse filesystem with the user code (it needs to be backed by GCS to handle persistence, say you're writing to a log file, it should be there next time you load your project). We then send the relevant setup command to pid1 (either init, or wakeup) which sets up the repl to start the user app, the repl, or what have you.

> What actually happens behind the scenes to make a deploy go live

We poll the container for published ports and the moment we see an open port we add a record to an etcd which stores the routing state. We then send a command to the client that we published a port, which will react by opening an iframe. Then the iframe or any request to the published url will hit our outer reverse proxy which will query etcd to find the container and if the container is alive we will send the traffic to the relevant container manager which has another reverse proxy which sends the traffic to the container.

If the container however is dead (from idling or because of an error) we revive via picking a container out of one the pools and going through the initialization phase described above.

Finally, we also host our own docker image registery so that we can push new images, whether new languages, new versions or what have you.

There is a lot more to talk about here so I or someone on the team will write a post soon.

https://news.ycombinator.com/item?id=16578597

@Almenon
Copy link
Collaborator

Almenon commented Jan 24, 2024

by containers i mean docker containers

@ra1nbow1
Copy link
Author

Thanks

@ra1nbow1
Copy link
Author

@Almenon how to use setTimeout if PythonShell returns a promise

@ra1nbow1
Copy link
Author

@Almenon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants