Skip to content

Commit

Permalink
Polling causes memory leak by retaining request objects indefinitely (#…
Browse files Browse the repository at this point in the history
…188)

* Upgrade deps

* Remove redundant condition

* Lint code

* Prevent broadcast stream memory leaks
  • Loading branch information
JosefWN committed Mar 23, 2024
1 parent ab3e660 commit de22855
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 23 deletions.
3 changes: 2 additions & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ linter:
- avoid_catching_errors
- avoid_classes_with_only_static_members
- avoid_double_and_int_checks
#- avoid_dynamic_calls
- avoid_dynamic_calls
- avoid_empty_else
- avoid_equals_and_hash_code_on_mutable_classes
- avoid_escaping_inner_quotes
Expand Down Expand Up @@ -98,6 +98,7 @@ linter:
- no_default_cases
- no_duplicate_case_values
- no_logic_in_create_state
- no_runtimeType_toString
- non_constant_identifier_names
- noop_primitive_operations
- null_check_on_nullable_type_parameter
Expand Down
6 changes: 4 additions & 2 deletions lib/src/coap_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,8 @@ class CoapClient {
request
..observe = ObserveRegistration.register.value
..maxRetransmit = maxRetransmit;
final responseStream = _sendWithStreamResponse(request).asBroadcastStream();
final responseStream = _sendWithStreamResponse(request)
.asBroadcastStream(onCancel: (final sub) => sub.cancel());
final relation = CoapObserveClientRelation(request, responseStream);
final resp = await _waitForResponse(request, responseStream);
if (!resp.hasOption<ObserveOption>()) {
Expand All @@ -543,7 +544,8 @@ class CoapClient {
final CoapRequest request, {
final CoapMulticastResponseHandler? onMulticastResponse,
}) async {
final responseStream = _sendWithStreamResponse(request).asBroadcastStream();
final responseStream = _sendWithStreamResponse(request)
.asBroadcastStream(onCancel: (final sub) => sub.cancel());
if (request.isMulticast) {
if (onMulticastResponse == null) {
throw ArgumentError('Missing onMulticastResponse argument');
Expand Down
15 changes: 8 additions & 7 deletions lib/src/event/coap_event_bus.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ abstract class CoapMessageEvent {
CoapMessage msg;

@override
String toString() => '$runtimeType: $msg';
String toString() => 'CoapMessageEvent: $msg';
}

abstract class CoapRequestEvent {
Expand All @@ -35,7 +35,7 @@ abstract class CoapRequestEvent {
CoapRequest req;

@override
String toString() => '$runtimeType: $req';
String toString() => 'CoapRequestEvent: $req';
}

abstract class CoapResponseEvent {
Expand All @@ -45,7 +45,7 @@ abstract class CoapResponseEvent {
CoapResponse resp;

@override
String toString() => '$runtimeType: $resp';
String toString() => 'CoapResponseEvent: $resp';
}

abstract class CoapExchangeEvent {
Expand All @@ -56,7 +56,7 @@ abstract class CoapExchangeEvent {

@override
String toString() =>
'$runtimeType:\nExchange for request ${exchange.request.id} '
'CoapExchangeEvent:\nExchange for request ${exchange.request.id} '
"(token '${exchange.request.tokenString}')";
}

Expand Down Expand Up @@ -158,15 +158,16 @@ class CoapMessageReceivedEvent {
InternetAddress address;

@override
String toString() => '$runtimeType:\n$coapMessage from ${address.address}';
String toString() =>
'CoapMessageReceivedEvent:\n$coapMessage from ${address.address}';
}

class CoapSocketInitEvent {
/// Construction
CoapSocketInitEvent();

@override
String toString() => '$runtimeType:\nSocket attempting to initialize';
String toString() => 'CoapSocketInitEvent:\nSocket attempting to initialize';
}

class CoapSocketErrorEvent {
Expand All @@ -180,7 +181,7 @@ class CoapSocketErrorEvent {
StackTrace stackTrace;

@override
String toString() => '$runtimeType:\n$error\n$stackTrace';
String toString() => 'CoapSocketErrorEvent:\n$error\n$stackTrace';
}

/// Event bus class
Expand Down
2 changes: 1 addition & 1 deletion lib/src/exceptions/coap_request_exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ abstract class CoapRequestException implements Exception {
CoapRequestException();

@override
String toString() => '$runtimeType: $failReason';
String toString() => 'CoapRequestException: $failReason';
}

/// This [Exception] is thrown when a CoAP request has timed out.
Expand Down
4 changes: 2 additions & 2 deletions lib/src/network/coap_inetwork.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class UnsupportedProtocolException implements Exception {
UnsupportedProtocolException(this.uriScheme);

@override
String toString() =>
'$runtimeType: Unsupported URI scheme $uriScheme encountered.';
String toString() => 'UnsupportedProtocolException: '
'Unsupported URI scheme $uriScheme encountered.';
}

/// Abstract networking class, allows different implementations for
Expand Down
2 changes: 1 addition & 1 deletion lib/src/option/coap_block_option.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ enum BlockOptionType {
abstract class CoapBlockOption extends IntegerOption
with OscoreOptionClassE, OscoreOptionClassU {
String get _szxErrorMessage =>
'Encountered reserved SZX value 7 in $runtimeType.';
'Encountered reserved SZX value 7 in CoapBlockOption.';

/// Base construction
CoapBlockOption(
Expand Down
2 changes: 1 addition & 1 deletion lib/src/option/coap_option_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ abstract class UnknownOptionException implements Exception {
UnknownOptionException(this.optionNumber, this.errorMessage);

@override
String toString() => '$runtimeType: $errorMessage';
String toString() => 'UnknownOptionException: $errorMessage';
}

/// [Exception] that is thrown when an unknown elective CoapOption number is
Expand Down
3 changes: 1 addition & 2 deletions lib/src/stack/layers/reliability.dart
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,7 @@ class _TransmissionContext {
// Do not retransmit a message if it has been acknowledged,
// rejected, canceled or already been retransmitted for the maximum
// number of times.
if (!_message.isRejected &&
_message.isActive &&
if (_message.isActive &&
failedTransmissionCount <=
(_message.maxRetransmit != 0
? _message.maxRetransmit
Expand Down
12 changes: 6 additions & 6 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ environment:
sdk: '>=3.0.0 <4.0.0'

dependencies:
build: ^2.4.0
collection: ^1.17.2
build: ^2.4.1
collection: ^1.18.0
convert: ^3.1.1
dtls2: ^0.16.0
event_bus: ^2.0.0
meta: ^1.9.0
path: ^1.8.3
meta: ^1.12.0
path: ^1.9.0
string_scanner: ^1.2.0
synchronized: ^3.1.0
typed_data: ^1.3.2
yaml: ^3.1.2

dev_dependencies:
build_runner: ^2.4.7
build_runner: ^2.4.8
lints: ^3.0.0
test: ^1.25.0
test: ^1.25.2

topics:
- iot
Expand Down

0 comments on commit de22855

Please sign in to comment.