dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.3k stars 1.59k forks source link

Memory leak runtime/bin/security_context_win.cc #54251

Closed yaminet1024 closed 11 months ago

yaminet1024 commented 12 months ago

This tracker is for issues related to:

Dart version and tooling diagnostic info

Dart 3.0.2 (stable) (Unknown timestamp) on "windows_x64" on windows / "Windows 10 Pro" 10.0 (Build 22631) locale is zh-CN

Whether you are using Windows, macOS, or Linux

Windows

problem

17018685559639

I found it when I was looking at the leaks in my Flutter Windows application.

a-siva commented 12 months ago

//cc @aam

aam commented 12 months ago

@yaminet1024 thanks for the report. Do you have small app that reproduces the problem? Do you have a flutter or standalone dart app? What is the screenshot of? What is that that you use for leak detection?

yaminet1024 commented 12 months ago

@yaminet1024 thanks for the report. Do you have small app that reproduces the problem? Do you have a flutter or standalone dart app? What is the screenshot of? What is that that you use for leak detection?

Thank you for your assistance. We use Visual Studio to debug our Flutter application. The screenshot provided is from the Visual Studio debugger focusing on memory usage. You can find more information about this tool at https://learn.microsoft.com/en-us/visualstudio/profiling/memory-usage-without-debugging2?view=vs-2022. I've noticed that the number of Dart SSL objects increases each time I open and close the Flutter windows. I plan to create a Flutter example to illustrate this issue. Additionally, could you guide me on how to debug a windows standalone Dart application to analyze memory?

aam commented 12 months ago

Aha, thanks.

Additionally, could you guide me on how to debug a windows standalone Dart application to analyze memory?

You should be able to run .dart programs from powershell using dart.exe that is part of flutter like so:

C> dart main.dart
/CN=storage.googleapis.com
/C=US/O=Google Trust Services LLC/CN=GTS CA 1C3
dc7198af64b5e40a69b2e8d8998e6d5e9aa02ab7748063d903f1d5887c5b1bcf *dartsdk-windows-x64-release.zip
C>

with main.dart having the following contents

import 'dart:io';

main() {
  HttpClient client = new HttpClient();
  client
      .getUrl(Uri.parse("https://storage.googleapis.com/dart-archive/channels/stable/release/3.2.3/sdk/dartsdk-windows-x64-release.zip.sha256sum"))
      .then((request) => request.close())
      .then((response) {
    print(response.certificate!.subject);
    print(response.certificate!.issuer);
    return response
        .fold<List<int>>(<int>[], (message, data) => message..addAll(data));
  }).then((message) {
    String received = new String.fromCharCodes(message);
    print(received);
    client.close();
  });
}

You can also download dart sdk separately from flutter from https://dart.dev/get-dart/archive

Profiling such apps with Visual Studio would involve launching dart.exe process with main.dart as an argument from Visual Studio.

yaminet1024 commented 12 months ago

Aha, thanks.

Additionally, could you guide me on how to debug a windows standalone Dart application to analyze memory?

You should be able to run .dart programs from powershell using dart.exe that is part of flutter like so:

C> dart main.dart
/CN=storage.googleapis.com
/C=US/O=Google Trust Services LLC/CN=GTS CA 1C3
dc7198af64b5e40a69b2e8d8998e6d5e9aa02ab7748063d903f1d5887c5b1bcf *dartsdk-windows-x64-release.zip
C>

with main.dart having the following contents

import 'dart:io';

main() {
  HttpClient client = new HttpClient();
  client
      .getUrl(Uri.parse("https://storage.googleapis.com/dart-archive/channels/stable/release/3.2.3/sdk/dartsdk-windows-x64-release.zip.sha256sum"))
      .then((request) => request.close())
      .then((response) {
    print(response.certificate!.subject);
    print(response.certificate!.issuer);
    return response
        .fold<List<int>>(<int>[], (message, data) => message..addAll(data));
  }).then((message) {
    String received = new String.fromCharCodes(message);
    print(received);
    client.close();
  });
}

You can also download dart sdk separately from flutter from https://dart.dev/get-dart/archive

Profiling such apps with Visual Studio would involve launching dart.exe process with main.dart as an argument from Visual Studio.

Here is the Flutter example that uses image.url, which can reproduce the memory leak issue. You can watch the video to see how to reproduce it

https://github.com/dart-lang/sdk/assets/23304639/9345fa41-4710-4ef9-87c9-7d897755e4ce

demo_win.zip

yaminet1024 commented 12 months ago

https://github.com/dart-lang/sdk/blob/296a7f642eb88007e93afd1cfd2921d62152e939/runtime/bin/security_context_win.cc#L122C9-L122C15

I've identified the issue: if the status is not equal to 0 in the line int status = X509_STORE_add_cert(store, root_cert);, then root_cert is never freed. However, when I call X509_free(root_cert) after X509_STORE_add_cert, X509 will not leak

The function X509_STORE_add_cert increases the reference count for the X509 certificate. If we do not free root_cert, it will always have a reference count remaining. You can refer to the documentation for more details at:

For X509_STORE_add_cert: https://www.openssl.org/docs/man1.1.1/man3/X509_STORE_add_cert.html For X509_free: https://www.openssl.org/docs/man3.2/man3/X509_free.html

aam commented 12 months ago

Thank you @yaminet1024 , you are right, X509_free(root_cert) call is missing. We should do the freeing right after the call to X509_STORE_add_cert, remove such calls from if (status == 0) then-branch. Do you want to submit a patch?

yaminet1024 commented 12 months ago

Thank you @yaminet1024 , you are right, X509_free(root_cert) call is missing. We should do the freeing right after the call to X509_STORE_add_cert, remove such calls from if (status == 0) then-branch. Do you want to submit a patch?

yes, I'd be happy to help! I hope I can be of assistance.

aam commented 12 months ago

Sounds good, see if you can follow https://github.com/dart-lang/sdk/blob/main/CONTRIBUTING.md to create either Github PR or Gerrit patch. Gerrit workflow is what dart sdk dev team uses internally, but might take a little more effort to set up compared to Github workflow.

afl-dev commented 3 months ago

Си

Ага, спасибо.

Кроме того, не могли бы вы подсказать мне, как отладить автономное приложение Dart для Windows для анализа памяти?

Вы должны иметь возможность запускать программы .dart из PowerShell с помощью dart.exe, который является частью flutter, например, так:

C> dart main.dart
/CN=storage.googleapis.com
/C=US/O=Google Trust Services LLC/CN=GTS CA 1C3
dc7198af64b5e40a69b2e8d8998e6d5e9aa02ab7748063d903f1d5887c5b1bcf *dartsdk-windows-x64-release.zip
C>

с main.dart, имеющим следующее содержимое

import 'dart:io';

main() {
  HttpClient client = new HttpClient();
  client
      .getUrl(Uri.parse("https://storage.googleapis.com/dart-archive/channels/stable/release/3.2.3/sdk/dartsdk-windows-x64-release.zip.sha256sum"))
      .then((request) => request.close())
      .then((response) {
    print(response.certificate!.subject);
    print(response.certificate!.issuer);
    return response
        .fold<List<int>>(<int>[], (message, data) => message..addAll(data));
  }).then((message) {
    String received = new String.fromCharCodes(message);
    print(received);
    client.close();
  });
}

Вы также можете загрузить dart sdk отдельно от flutter с https://dart.dev/get-dart/archive Профилирование таких приложений с помощью Visual Studio будет включать запуск dart.exeпроцесса с использованием main.dartв качестве аргумента из Visual Studio.

Вот пример Flutter, который использует image.url, который может воспроизвести проблему утечки памяти. Вы можете посмотреть видео, чтобы увидеть, как это воспроизвести

играть.mp4 demo_win.zip

the memory leak is reproducible again, windows and linux

aam commented 3 months ago

@afl-dev wrote

the memory leak is reproducible again, windows and linux

Since the original issue with security context leaking was fixed, please raisea new issue in this tracker describing the problem you are experiencing. Please specify flutter version you are using. The app from demo_win.zip seems to work as expected on 3.5.0-176.0. mp4-file you mentioned is not available.