From c6b406b37246b7dfd2b5a2cc543bfcbc1b24710a Mon Sep 17 00:00:00 2001 From: Christopher Fujino Date: Fri, 21 Jun 2024 19:19:34 -0700 Subject: [PATCH 1/4] delete old log file if it exceeds kMaxLogFileSize of 25MiB --- pkgs/unified_analytics/lib/src/constants.dart | 5 + .../lib/src/log_handler.dart | 14 ++- .../test/log_handler_test.dart | 92 +++++++++++++++++++ 3 files changed, 108 insertions(+), 3 deletions(-) diff --git a/pkgs/unified_analytics/lib/src/constants.dart b/pkgs/unified_analytics/lib/src/constants.dart index 9ac9d1ac..73c769b2 100644 --- a/pkgs/unified_analytics/lib/src/constants.dart +++ b/pkgs/unified_analytics/lib/src/constants.dart @@ -78,6 +78,11 @@ const String kGoogleAnalyticsMeasurementId = 'G-04BXPVBCWJ'; /// How many data records to store in the log file. const int kLogFileLength = 2500; +/// The maximum allowed size of the telemetry log file. +/// +/// 25 MiB. +const int kMaxLogFileSize = 25 * (1 << 20); + /// Filename for the log file to persist sent events on user's machine. const String kLogFileName = 'dart-flutter-telemetry.log'; diff --git a/pkgs/unified_analytics/lib/src/log_handler.dart b/pkgs/unified_analytics/lib/src/log_handler.dart index d611b73a..c8c04049 100644 --- a/pkgs/unified_analytics/lib/src/log_handler.dart +++ b/pkgs/unified_analytics/lib/src/log_handler.dart @@ -272,10 +272,18 @@ class LogHandler { /// This will keep the max number of records limited to equal to /// or less than [kLogFileLength] records. void save({required Map data}) { - var records = logFile.readAsLinesSync(); - final content = '${jsonEncode(data)}\n'; - try { + final stat = logFile.statSync(); + List records; + if (stat.size > kMaxLogFileSize) { + logFile.deleteSync(); + logFile.createSync(); + records = []; + } else { + records = logFile.readAsLinesSync(); + } + final content = '${jsonEncode(data)}\n'; + // When the record count is less than the max, add as normal; // else drop the oldest records until equal to max if (records.length < kLogFileLength) { diff --git a/pkgs/unified_analytics/test/log_handler_test.dart b/pkgs/unified_analytics/test/log_handler_test.dart index 3cacf472..05fb466d 100644 --- a/pkgs/unified_analytics/test/log_handler_test.dart +++ b/pkgs/unified_analytics/test/log_handler_test.dart @@ -2,9 +2,12 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. +import 'dart:convert'; + import 'package:file/file.dart'; import 'package:file/memory.dart'; import 'package:path/path.dart' as p; +import 'package:test/fake.dart'; import 'package:test/test.dart'; import 'package:unified_analytics/src/constants.dart'; @@ -212,6 +215,47 @@ void main() { logHandler.save(data: {}); }); + test('deletes log file larger than kMaxLogFileSize', () async { + var deletedLargeLogFile = false; + var wroteDataToLogFile = false; + const data = {}; + final logFile = _FakeFile('log.txt') + .._deleteSyncImpl = (() => deletedLargeLogFile = true) + .._createSyncImpl = () {} + .._statSyncImpl = (() => _FakeFileStat(kMaxLogFileSize + 1)) + .._writeAsStringSync = (contents, {mode = FileMode.append}) { + expect(contents.trim(), data.toString()); + expect(mode, FileMode.writeOnlyAppend); + wroteDataToLogFile = true; + }; + final logHandler = LogHandler(logFile: logFile); + + logHandler.save(data: data); + expect(deletedLargeLogFile, isTrue); + expect(wroteDataToLogFile, isTrue); + }); + + test('does not delete log file if smaller than kMaxLogFileSize', () async { + var wroteDataToLogFile = false; + const data = {}; + final logFile = _FakeFile('log.txt') + .._deleteSyncImpl = + (() => fail('called logFile.deleteSync() when file was less than ' + 'kMaxLogFileSize')) + .._createSyncImpl = () {} + .._readAsLinesSyncImpl = (() => ['three', 'previous', 'lines']) + .._statSyncImpl = (() => _FakeFileStat(kMaxLogFileSize - 1)) + .._writeAsStringSync = (contents, {mode = FileMode.append}) { + expect(contents.trim(), data.toString()); + expect(mode, FileMode.writeOnlyAppend); + wroteDataToLogFile = true; + }; + final logHandler = LogHandler(logFile: logFile); + + logHandler.save(data: data); + expect(wroteDataToLogFile, isTrue); + }); + test('Catching cast errors for each log record silently', () async { // Write a json array to the log file which will cause // a cast error when parsing each line @@ -290,3 +334,51 @@ void main() { expect(newString, testString); }); } + +class _FakeFileStat extends Fake implements FileStat { + _FakeFileStat(this.size); + + @override + final int size; +} + +class _FakeFile extends Fake implements File { + _FakeFile(this.path); + + List Function()? _readAsLinesSyncImpl; + + @override + List readAsLinesSync({Encoding encoding = utf8}) => + _readAsLinesSyncImpl!(); + + @override + final String path; + + FileStat Function()? _statSyncImpl; + + @override + FileStat statSync() => _statSyncImpl!(); + + void Function()? _deleteSyncImpl; + + @override + void deleteSync({bool recursive = false}) => _deleteSyncImpl!(); + + void Function()? _createSyncImpl; + + @override + void createSync({bool recursive = false, bool exclusive = false}) { + return _createSyncImpl!(); + } + + void Function(String contents, {FileMode mode})? _writeAsStringSync; + + @override + void writeAsStringSync( + String contents, { + FileMode mode = FileMode.write, + Encoding encoding = utf8, + bool flush = false, + }) => + _writeAsStringSync!(contents, mode: mode); +} From 885e1e6c8d98325addfc97daacc6852ad857828a Mon Sep 17 00:00:00 2001 From: Christopher Fujino Date: Mon, 24 Jun 2024 16:55:37 -0700 Subject: [PATCH 2/4] update changelog --- pkgs/unified_analytics/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkgs/unified_analytics/CHANGELOG.md b/pkgs/unified_analytics/CHANGELOG.md index b28088a0..744a6bed 100644 --- a/pkgs/unified_analytics/CHANGELOG.md +++ b/pkgs/unified_analytics/CHANGELOG.md @@ -1,3 +1,7 @@ +## 6.1.2 + +- Avoid opening large telemetry log files to prevent out of memory errors. + ## 6.1.1 - Fixed bug where calling `Analytics.send` could result in a `FileSystemException` when unable to write to a log file. From 772a983f5aff0e788aca7d226eea6ad56f3890fc Mon Sep 17 00:00:00 2001 From: Christopher Fujino Date: Mon, 24 Jun 2024 16:57:04 -0700 Subject: [PATCH 3/4] bump version in pubspec.yaml --- pkgs/unified_analytics/pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/unified_analytics/pubspec.yaml b/pkgs/unified_analytics/pubspec.yaml index 11c778b3..9ea78eec 100644 --- a/pkgs/unified_analytics/pubspec.yaml +++ b/pkgs/unified_analytics/pubspec.yaml @@ -4,7 +4,7 @@ description: >- to Google Analytics. # When updating this, keep the version consistent with the changelog and the # value in lib/src/constants.dart. -version: 6.1.1 +version: 6.1.2 repository: https://github.com/dart-lang/tools/tree/main/pkgs/unified_analytics environment: From 39f44f69fbb302f4a4774bc8d962ab6cb6a79274 Mon Sep 17 00:00:00 2001 From: Christopher Fujino Date: Mon, 24 Jun 2024 16:59:30 -0700 Subject: [PATCH 4/4] update constants.dart --- pkgs/unified_analytics/lib/src/constants.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/unified_analytics/lib/src/constants.dart b/pkgs/unified_analytics/lib/src/constants.dart index 73c769b2..26c6191d 100644 --- a/pkgs/unified_analytics/lib/src/constants.dart +++ b/pkgs/unified_analytics/lib/src/constants.dart @@ -87,7 +87,7 @@ const int kMaxLogFileSize = 25 * (1 << 20); const String kLogFileName = 'dart-flutter-telemetry.log'; /// The current version of the package, should be in line with pubspec version. -const String kPackageVersion = '6.1.1'; +const String kPackageVersion = '6.1.2'; /// The minimum length for a session. const int kSessionDurationMinutes = 30;