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

Unintended Action Override #489

Open
Depetrol opened this issue Oct 10, 2024 · 0 comments
Open

Unintended Action Override #489

Depetrol opened this issue Oct 10, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@Depetrol
Copy link
Collaborator

When two actions are triggered at about the same time, there is a chance that the later action value will override the previous action value. Example:

target Python {
    coordination: decentralized,
    // logging: debug
  }
  
  preamble {=
    from fastapi import FastAPI, Request, HTTPException
    from fastapi.middleware.cors import CORSMiddleware
    import threading
    import uvicorn
    import asyncio
    import uuid
  =}
  
  reactor WebServer(bank_index=0, STA=0) {
    state app
    state logs
    physical action addlog_action
  
    reaction(startup) -> addlog_action {=
      self.logs = []
      self.app = FastAPI()
      self.app.add_middleware(
          CORSMiddleware,
          allow_origins=["*"],
          allow_credentials=True,
          allow_methods=["*"],
          allow_headers=["*"],
      )
      @self.app.post("/log")
      async def addlog(request: Request):
          log_message = (await request.json())["log"]
          print(f"Received log: {log_message}")
          addlog_action.schedule(0, log_message)
          return {"status": "success"}
  
      def run_fastapi_app():
          print(f"[WebServer{self.bank_index}] FastAPI server starting")
          uvicorn.run(self.app, host="127.0.0.1", port=5000+self.bank_index, log_level="warning")
      fastapi_thread = threading.Thread(target=run_fastapi_app)
      fastapi_thread.start()
    =}
  
    reaction(addlog_action) {=
        if addlog_action.value in self.logs:
            print(f"Duplicate Action: {addlog_action.value}")
            raise Exception("Duplicate Action")
        else:
            print(f"Action: {addlog_action.value}")
            self.logs.append(addlog_action.value)
    =}
  }
  
  main reactor {
    server = new WebServer()
  }

trigger the physical action with this python script:

import requests
import time
import threading

def send_log(log_message, port=5000):
    url = f"http://127.0.0.1:{port}/log"
    log_data = {"log": f"{log_message}"}

    def send_request():
        try:
            requests.post(url, json=log_data, timeout=100)  # Send request without waiting for response
        except Exception as e:
            print(f"Error sending log: {e}")

    thread = threading.Thread(target=send_request)
    thread.start()

if __name__ == "__main__":
    for i in range(50):
        send_log(f"{i}", port=5000)
@Depetrol Depetrol added the bug Something isn't working label Oct 11, 2024
@Depetrol Depetrol changed the title Duplicated Actions Unintended Action Override Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant