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

Segmentation faults occur when using async #141

Open
Nabil372 opened this issue Sep 14, 2023 · 1 comment
Open

Segmentation faults occur when using async #141

Nabil372 opened this issue Sep 14, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@Nabil372
Copy link

Describe the bug

Segmentation faults occur during the following scenario:

  1. a websocket connection is made to an echo endpoint that uses async
  2. A high volume of messages are sent back and forth
  3. the websocket connection disconnects.
  4. When 3. happens the socketify instance segfaults.

Segfaults only occur when awaiting an async function in the socketify websocket message function.

Note: I also get segfaults before the connections are closed when sending high volumes of messages from 2 connections.

To Reproduce

simple example of socketify websocket server

import asyncio
from typing import Union

from socketify import App, CompressOptions, OpCode, WebSocket


async def async_function():
    await asyncio.sleep(1)


async def ws_message(ws: WebSocket, message: Union[str, bytes], opcode: OpCode):
    await async_function()
    ws.cork_send({"message": message})


app = App()

app.ws(
    "/echo",
    {
        "compression": CompressOptions.DISABLED,
        "max_payload_length": 16 * 1024,  # 16KB
        "idle_timeout": 60,  # 1 minute,
        "message": ws_message,
        "drain": lambda _: print("draining"),
    },
)

app.listen(
    3000,
    lambda config: print("Listening on port %d" % config.port),
)
app.run()
const WebSocket = require('ws');


const url = `ws://localhost:3000/echo`;
console.log(`Connecting to URL: ${url}`);

async function run() {
    const ws = new WebSocket(url);

    ws.on('message', function incoming(data) {
        console.log(data.toString());
    });

    ws.on('pong', function heartbeat() {
        ws.isAlive = true;
    });

    ws.on('error', function error(err) {
        console.error(err);
    });

    ws.on('close', function close() {
        console.log('connection closed');
    });

    // send a message every 5ms
    setInterval(() => {
        ws.send('Hello!');
    },5);
}

run();

When the nodejs script receives a SIGINT the following occurs:

$ python -m socketify main:app
Listening on port 3000
[1]    9977 segmentation fault  python -m socketify main:app

Expected behavior
socketify should not have segfaulted

Desktop (please complete the following information):
I ran this on Ventura MacOS 13.5 (22G74), using python 3.11.5 and node 18.16.0

@cirospaciari cirospaciari added the bug Something isn't working label Feb 1, 2024
@cirospaciari
Copy link
Owner

Sounds like a life cycle issue with CFFI

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

2 participants