dart_tdlib icon indicating copy to clipboard operation
dart_tdlib copied to clipboard

listening to client.incoming() causes crash

Open rafalbednarczuk opened this issue 5 years ago • 10 comments

  await for (final update in client.incoming()) {
      print(update.tdType);
    }

this part of your example causes this output


lib/main.dart: Warning: Interpreting this as package URI, 'package:tdlib/main.dart'.
[ 3][t 4][1576026651.009212255][Td.cpp:4657][#1][!Td][&td_requests]	Sending update: updateOption {
  name = "version"
  value = optionValueString {
    value = "1.5.1"
  }
}
[ 3][t 4][1576026651.009475946][Td.cpp:4657][#1][!Td][&td_requests]	Sending update: updateAuthorizationState {
  authorization_state = authorizationStateWaitTdlibParameters {
  }
}
[ 3][t 0][1576026651.024739504][Client.cpp:270][&td_requests]	Begin to wait for updates with timeout 2.000000
[ 3][t 0][1576026651.024761677][Client.cpp:276][&td_requests]	End to wait for updates, returning object 0 0x7ff4a0101a40
[ 3][t 0][1576026651.043768406][Client.cpp:270][&td_requests]	Begin to wait for updates with timeout 2.000000updateOption

updateAuthorizationState
[ 3][t 0][1576026651.043790102][Client.cpp:276][&td_requests]	End to wait for updates, returning object 0 0x7ff4a0102000
[ 3][t 0][1576026651.045000315][Client.cpp:270][&td_requests]	Begin to wait for updates with timeout 2.000000
[ 3][t 0][1576026653.047059774][Client.cpp:276][&td_requests]	End to wait for updates, returning object 0 (nil)

===== CRASH =====
si_signo=Segmentation fault(11), si_code=1, si_addr=(nil)
version=2.7.0-dev.2.1.flutter-a9c77229c2 (Wed Dec 4 11:26:20 2019 +0000) on "linux_x64"
thread=1895, isolate=main(0x30081e0)
  pc 0x00007ff4e3768eff fp 0x00007ff4d297e4b8 Unknown symbol
  pc 0x00007ff4e37678d5 fp 0x00007ff4d297e500 Unknown symbol
  pc 0x00007ff4e376770a fp 0x00007ff4d297e538 Unknown symbol
  pc 0x00007ff4e3766d89 fp 0x00007ff4d297e5a0 Unknown symbol
  pc 0x00007ff4e376688e fp 0x00007ff4d297e638 Unknown symbol
  pc 0x00007ff4e37299b7 fp 0x00007ff4d297e670 Unknown symbol
  pc 0x00007ff4e372b106 fp 0x00007ff4d297e6a8 Unknown symbol
  pc 0x00007ff4e376648f fp 0x00007ff4d297e730 Unknown symbol
  pc 0x00007ff4e3765ce8 fp 0x00007ff4d297e7c0 Unknown symbol
  pc 0x00007ff4e37299b7 fp 0x00007ff4d297e7f8 Unknown symbol
  pc 0x00007ff4e37653f0 fp 0x00007ff4d297e830 Unknown symbol
  pc 0x00007ff4e37651fe fp 0x00007ff4d297e868 Unknown symbol
  pc 0x00007ff4e37650c4 fp 0x00007ff4d297e8b0 Unknown symbol
  pc 0x00007ff4e3764d61 fp 0x00007ff4d297e8f0 Unknown symbol
  pc 0x00007ff4e376492b fp 0x00007ff4d297e930 Unknown symbol
  pc 0x00007ff4e3764827 fp 0x00007ff4d297e970 Unknown symbol
  pc 0x00007ff4e376462a fp 0x00007ff4d297e9b0 Unknown symbol
  pc 0x00007ff4e37289bd fp 0x00007ff4d297e9d8 Unknown symbol
  pc 0x00007ff4e3f0145c fp 0x00007ff4d297ea48 Unknown symbol
-- End of DumpStackTrace

If I delete this part then there is no error, but I don't have updates

rafalbednarczuk avatar Dec 11 '19 01:12 rafalbednarczuk

So it seems to be crashing after sending the authorizationStateWaitTdlibParameters message and failing to see a response (when TDLib doesn't receive a response before the timeout, it just returns a null reference, which causes the segmentation fault). According to the TDLib documentation, you're supposed to respond to this by sending a setTdlibParameters command with the appropriate fields, which is illustrated in the README example right before the await-for-loop:

client.authorizationState.listen((state) {
  if (state is td.AuthorizationStateWaitTdLibParameters) {
    await client.send(client.tdlibParams);
  }
});

Now, while writing this response I noticed that I mistakenly wrote "authentication" instead of "authorization" in the README. This will be remedied immediately. I also recognize that TelegramClient.setTdLibParams is confusingly named, so I think I'll rename it to defineTdlibParams (since it doesn't actually send the setTdlibParameters command).

I can't see the rest of your code so if you are already handling the authorization state and aren't having any name confusion then you'll have to show me more of the code.

periodicaidan avatar Dec 16 '19 18:12 periodicaidan

import 'package:tdlib/src/api/functions.dart';
import 'package:tdlib/tdlib.dart' as td;
import 'main.reflectable.dart';

void main() async {
  initializeReflectable();
  // Create a client
  final client = td.TelegramClient("/home/rafal/telegram/");
  client.setTdLibParams(
      apiId: MY_API_ID,
      apiHash: MY_API_HASH,
      deviceModel: "Nexus 5",
      systemVersion: "6.0.1");
  try {
    // Get a stream to handle changes to the authorization state
    client.authorizationState.listen((state) async {
      if (state is td.AuthorizationStateWaitTdlibParameters) {
        await client.send(SetTdlibParameters(client.tdlibParams));
      }
    });

    await for (var update in client.incoming()) {
      // Handle each incoming update
    }
  } finally {
    // Be sure to close the client
    client.close();
  }
}

There is my code which causes crashes. I cloned this repo and added this main file to lib/

rafalbednarczuk avatar Dec 21 '19 17:12 rafalbednarczuk

Do you have any idea? I can't go forward anywhere with this bug.

rafalbednarczuk avatar Dec 26 '19 19:12 rafalbednarczuk

I want to keep working through this but tentatively I'm struggling to recreate this crash. I've copied the logic of your code exactly, and while it does crash (because updates aren't being handled; see the terminal output below) it doesn't do so on the authorizationStateWaitTdlibParameters request. I've made a few uncommitted changes to this library recently that I'll push soon but none of them would require your code to change; TelegramClient.setTdLibParameters() will be deprecated but I'll be leaving it for a few more versions so it'll work exactly as it does now.

[ 3][t 4][1577388663.302275658][Td.cpp:4657][#1][!Td][&td_requests]     Sending update: updateOption {
  name = "version"
  value = optionValueString {
    value = "1.5.1"
  }
}
[ 3][t 4][1577388663.302403212][Td.cpp:4657][#1][!Td][&td_requests]     Sending update: updateAuthorizationState {
  authorization_state = authorizationStateWaitTdlibParameters {
  }
}
[ 3][t 0][1577388663.312212944][Client.cpp:270][&td_requests]   Begin to wait for updates with timeout 2.000000
[ 3][t 0][1577388663.312244177][Client.cpp:276][&td_requests]   End to wait for updates, returning object 0 0x558950b6fa70
[ 3][t 0][1577388663.329548359][Client.cpp:270][&td_requests]   Begin to wait for updates with timeout 2.000000
[ 3][t 0][1577388663.329573870][Client.cpp:276][&td_requests]   End to wait for updates, returning object 0 0x55895027c410

-------------------{ YOURS CRASHES HERE }-------------------

[ 3][t 4][1577388663.340855360][Td.cpp:3548][#1][!Td][&td_requests]     Receive request 1: setTdlibParameters {
  parameters = tdlibParameters {
    use_test_dc = false
    database_directory = "tdb"
    files_directory = ""
    use_file_database = false
    use_chat_info_database = false
    use_message_database = true
    use_secret_chats = true
    api_id = REDACTED
    api_hash = REDACTED
    system_language_code = "en-US"
    device_model = "Nexus 5"
    system_version = "6.0.1"
    application_version = "0.0.1"
    enable_storage_optimizer = true
    ignore_file_names = false
  }
}
[ 3][t 0][1577388663.340897083][Client.cpp:270][&td_requests]   Begin to wait for updates with timeout 2.000000
[ 3][t 4][1577388663.341100216][Td.cpp:4657][#1][!Td][&td_requests]     Sending update: updateAuthorizationState {
  authorization_state = authorizationStateWaitEncryptionKey {
    is_encrypted = false
  }
}
[ 3][t 4][1577388663.341127157][Td.cpp:4672][#1][!Td][&td_requests]     Sending result for request 1: ok {
}
[ 3][t 0][1577388663.341130495][Client.cpp:276][&td_requests]   End to wait for updates, returning object 0 0x55895027c450
[ 3][t 0][1577388663.342518806][Client.cpp:270][&td_requests]   Begin to wait for updates with timeout 2.000000
[ 3][t 0][1577388663.342553377][Client.cpp:276][&td_requests]   End to wait for updates, returning object 1 0x55895027a0b0
[ 3][t 0][1577388663.343151569][Client.cpp:270][&td_requests]   Begin to wait for updates with timeout 2.000000
[ 3][t 0][1577388665.345277548][Client.cpp:276][&td_requests]   End to wait for updates, returning object 0 (nil)

===== CRASH =====
si_signo=Segmentation fault(11), si_code=1, si_addr=(nil)
version=2.7.0 (Unknown timestamp) on "linux_x64"
thread=16538, isolate=main(0x5589502a6c00)
...

To test a bit further, I created a new project and included this project as a dependency in the pubspec instead of cloning it into the lib folder, again using the same code you gave me. Still works fine. I dunno if it'll change anything but if you want to try that, you can add this to your pubspec:

dependencies:
  tdlib: 
    git: git://github.com/periodicaidan/dart_tdlib

And then add a build.yaml file in the same directory as your pubspec with the following:

targets:
  $default:
    builders:
      reflectable:
        generate_for:
          - lib/main.dart
        options:
          formatted: true

And you can still build the reflectables using pub run build_runner build.

This could also be a TDLib problem but it's hard to tell. If you can't figure anything out you may want to forward this issue to them.

Sorry for the delayed reply, I've been travelling and doing holiday festivites the past few days.

periodicaidan avatar Dec 27 '19 18:12 periodicaidan

@periodicaidan Can you push somewhere the new project you created? So I can clone, change my tdlib path and try to run?

rafalbednarczuk avatar Dec 28 '19 02:12 rafalbednarczuk

I just finalized and pushed it, you can give it a go now

periodicaidan avatar Dec 29 '19 15:12 periodicaidan

@periodicaidan I don't see any new commits nor any new repositories on your github.

rafalbednarczuk avatar Dec 29 '19 18:12 rafalbednarczuk

Here's the commit: c700c6ffca72d9b1cd1dd2e613f8485c076ceaad

periodicaidan avatar Dec 29 '19 19:12 periodicaidan

I executed it as Dart command line application and gives me the same error. What platform are you executing on?

rafalbednarczuk avatar Dec 30 '19 07:12 rafalbednarczuk

I'm also executing it as a command line application. Dart VM version 2.7.0 on x86_64 Linux (Ubuntu).

periodicaidan avatar Dec 31 '19 18:12 periodicaidan