Skip to content

Segmentation faults occur when using async #141

Open
@Nabil372

Description

@Nabil372

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions