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

TelegramClient is leaking async ops #43

Open
baumschubser opened this issue Apr 1, 2023 · 1 comment
Open

TelegramClient is leaking async ops #43

baumschubser opened this issue Apr 1, 2023 · 1 comment

Comments

@baumschubser
Copy link

My deno test with grm fails, because my TelegramClient.invoke(…) is not finishing, but leaking some resource.
Try to run this test (with your own API_ID and API_HASH obviously) with deno test --allow-net:

import { superoak } from "https://deno.land/x/[email protected]/mod.ts";
import { Application, Router } from "https://deno.land/x/[email protected]/mod.ts";
import { StringSession, TelegramClient, Api } from "https://deno.land/x/[email protected]/mod.ts";

const API_ID = xxxx;
const API_HASH = 'yyyyyyy';
const telNo = `999662${Math.floor(1000 + Math.random() * 9000)}`;

function getApp() {
    const router = new Router();
    router.get('/', async c => {
        const client = new TelegramClient(new StringSession(''), API_ID, API_HASH);
        await client.connect();
        await client.invoke(
            new Api.auth.SendCode({
                phoneNumber: telNo,
                apiId: API_ID,
                apiHash: API_HASH,
                settings: new Api.CodeSettings({}),
            })
        );
        c.response.body = 'hi';
    });

    const app = new Application();
    app.use(router.routes());
    app.use(router.allowedMethods());
    return app;
}

Deno.test("Gram bug", async t => {
    await t.step("GET /", async () => {
        await (await superoak(getApp())).get("/").expect('hi');
    });
});

I get this error message:

GET / ... FAILED (2s)
    error: AssertionError: Test case is leaking async ops.
    
     - 1 async operation to op_read was started in this test, but never completed.
     - 2 async operations to sleep for a duration were started in this test, but never completed. This is often caused by not cancelling a `setTimeout` or `setInterval` call.

You can suppress this error with Deno.test options, but this only works for the first TelegramClient initalization. If you have multiple tests that do new TelegramClient, all tests except the first one fail with Telegram responding with 401: AUTH_KEY_UNREGISTERED.

Not sure if it is a bug that TelegramClient does not really finish itself, or if there is a command to do that (I tried TelegramClient.disconnect() and TelegramClient.destroy()).

@baumschubser baumschubser changed the title Deno test is leaking async ops TelegramClient is leaking async ops Apr 1, 2023
@baumschubser
Copy link
Author

Sorry for this example code that also involves oak and superoak. If we remove that and just have a test function like this, we don't get the error message but just a "failed" and a non-exiting test:

Deno.test("Gram bug", async () => {
    const client = new TelegramClient(new StringSession(''), API_ID, API_HASH);
    await client.connect();
    await client.invoke(
        new Api.auth.SendCode({
            phoneNumber: telNo,
            apiId: API_ID,
            apiHash: API_HASH,
            settings: new Api.CodeSettings({}),
        })
    );
    assertEquals(true, true);
});

Maybe it has something to do with this issue in gram.js?

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

No branches or pull requests

1 participant