After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 793880 - gnetworkmonitor: Minor fixes based on code review
gnetworkmonitor: Minor fixes based on code review
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: gio
unspecified
Other All
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2018-02-27 14:29 UTC by Philip Withnall
Modified: 2018-02-28 17:41 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
gnetworkmonitornetlink: Fix potential GMainContext issue (2.95 KB, patch)
2018-02-27 14:30 UTC, Philip Withnall
committed Details | Review
gnetworkmonitornetlink: Use a coarser-grained timer for dumps (1.31 KB, patch)
2018-02-27 14:30 UTC, Philip Withnall
committed Details | Review
gnetworkmonitornetlink: Refactor some code to reduce duplication (4.14 KB, patch)
2018-02-27 14:30 UTC, Philip Withnall
committed Details | Review
gnetworkmonitornetlink: Fix memory leaks on error paths (1.89 KB, patch)
2018-02-27 14:30 UTC, Philip Withnall
committed Details | Review
gnetworkmonitornetlink: Fix a memory leak in unusual circumstances (1.05 KB, patch)
2018-02-27 14:30 UTC, Philip Withnall
committed Details | Review
gnetworkmonitornm: Fix some minor GVariant memory leaks (1011 bytes, patch)
2018-02-27 14:30 UTC, Philip Withnall
committed Details | Review
gnetworkmonitornm: Use g_strv_contains() rather than reinventing it (1.23 KB, patch)
2018-02-27 14:30 UTC, Philip Withnall
committed Details | Review

Description Philip Withnall 2018-02-27 14:29:57 UTC
I ended up doing a code review of GNetworkMonitor and its in-tree extension point implementations, and found a few issues worth fixing. Patches coming.
Comment 1 Philip Withnall 2018-02-27 14:30:02 UTC
Created attachment 369026 [details] [review]
gnetworkmonitornetlink: Fix potential GMainContext issue

Previously, the GSource would be attached to whatever GMainContext was
the thread default at the time; but that might no longer be the same as
the default at the time of constructing the GNetworkMonitor.

Save the default from construction time, so that source callbacks are
always invoked in the same GMainContext.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
Comment 2 Philip Withnall 2018-02-27 14:30:09 UTC
Created attachment 369027 [details] [review]
gnetworkmonitornetlink: Use a coarser-grained timer for dumps

By using g_timeout_source_new_seconds(), we can let timer wakeups be
coalesced by the scheduler, and reduce power consumption a bit. This
shouldn’t really affect the accuracy of the network monitoring.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
Comment 3 Philip Withnall 2018-02-27 14:30:15 UTC
Created attachment 369028 [details] [review]
gnetworkmonitornetlink: Refactor some code to reduce duplication

This should introduce no functional changes. Factor out some common
code, flip some arguments around to use the more conventional (data,
length) order, and move some memory management calls out of
if-blocks.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
Comment 4 Philip Withnall 2018-02-27 14:30:21 UTC
Created attachment 369029 [details] [review]
gnetworkmonitornetlink: Fix memory leaks on error paths

Use a common error handler to avoid leaks on the error paths.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
Comment 5 Philip Withnall 2018-02-27 14:30:25 UTC
Created attachment 369030 [details] [review]
gnetworkmonitornetlink: Fix a memory leak in unusual circumstances

If the GNetworkMonitorNetlink is finalised part-way through a dump
(after request_dump() is called, but before finish_dump() is called),
dump_networks was leaked. Fix that.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
Comment 6 Philip Withnall 2018-02-27 14:30:32 UTC
Created attachment 369031 [details] [review]
gnetworkmonitornm: Fix some minor GVariant memory leaks

Signed-off-by: Philip Withnall <withnall@endlessm.com>
Comment 7 Philip Withnall 2018-02-27 14:30:37 UTC
Created attachment 369032 [details] [review]
gnetworkmonitornm: Use g_strv_contains() rather than reinventing it

This introduces no functional changes.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
Comment 8 Krzesimir Nowak 2018-02-27 14:58:30 UTC
Review of attachment 369028 [details] [review]:

::: gio/gnetworkmonitornetlink.c
@@ +227,3 @@
   network = g_inet_address_mask_new (dest_addr, dest_len, NULL);
   g_object_unref (dest_addr);
+  g_return_val_if_fail (network != NULL, NULL);

Should this "return if fail" be moved to the consumers of the create_inet_address_mask? Otherwise it will yell here about "network" being NULL, but "add_network" and "remove_network" will happily run g_object_ref etc on it.
Comment 9 Krzesimir Nowak 2018-02-27 15:00:49 UTC
Review of attachment 369029 [details] [review]:

::: gio/gnetworkmonitornetlink.c
@@ +313,3 @@
     {
       g_warning ("Error on netlink socket: %s", error->message);
+      g_clear_error (&error);

I don't see the full context of this function, but maybe g_clear_error could also be called after "done:" label?
Comment 10 Philip Withnall 2018-02-27 15:06:17 UTC
(In reply to Krzesimir Nowak from comment #8)
> Review of attachment 369028 [details] [review] [review]:
> 
> ::: gio/gnetworkmonitornetlink.c
> @@ +227,3 @@
>    network = g_inet_address_mask_new (dest_addr, dest_len, NULL);
>    g_object_unref (dest_addr);
> +  g_return_val_if_fail (network != NULL, NULL);
> 
> Should this "return if fail" be moved to the consumers of the
> create_inet_address_mask? Otherwise it will yell here about "network" being
> NULL, but "add_network" and "remove_network" will happily run g_object_ref
> etc on it.

Hmm, borderline, but I suppose so. I was basically thinking that all bets were off at that point, but we could try to be nice.

(In reply to Krzesimir Nowak from comment #9)
> Review of attachment 369029 [details] [review] [review]:
> 
> ::: gio/gnetworkmonitornetlink.c
> @@ +313,3 @@
>      {
>        g_warning ("Error on netlink socket: %s", error->message);
> +      g_clear_error (&error);
> 
> I don't see the full context of this function, but maybe g_clear_error could
> also be called after "done:" label?

I’d prefer to treat the scope of the GError allocations a little more locally.
Comment 11 Philip Withnall 2018-02-27 15:08:10 UTC
(In reply to Krzesimir Nowak from comment #8)
> Review of attachment 369028 [details] [review] [review]:
> 
> ::: gio/gnetworkmonitornetlink.c
> @@ +227,3 @@
>    network = g_inet_address_mask_new (dest_addr, dest_len, NULL);
>    g_object_unref (dest_addr);
> +  g_return_val_if_fail (network != NULL, NULL);
> 
> Should this "return if fail" be moved to the consumers of the
> create_inet_address_mask? Otherwise it will yell here about "network" being
> NULL, but "add_network" and "remove_network" will happily run g_object_ref
> etc on it.

I’ve made that change locally. Doesn’t seem much point in updating the entire patch set on Bugzilla for it, but I can if people want.
Comment 12 Will Thompson 2018-02-27 17:57:22 UTC
Review of attachment 369029 [details] [review]:

I think the placement of `g_clear_error (&error)` is fine as it is here. If it were moved to the 'done:' block I'd suggest moving the three identical "error on netlink socket" errors there too, ie:

  if (len < 0)
    {
      retval = FALSE;
      goto done;
    }

  ...

  done:
    if (error != NULL)
      g_warning ("Error on netlink socket: %s", error->message);
    g_clear_error (&error);
    ...

Though this would split the error handling above and below a ~100 line loop. Up to you, Philip.
Comment 13 Will Thompson 2018-02-27 18:00:03 UTC
I agree with the suggestion to move the `g_return_if_fail` out to the consumers of `create_inet_address_mask`.

Those two comments aside, all patches LGTM.
Comment 14 Philip Withnall 2018-02-28 17:38:50 UTC
(In reply to Will Thompson from comment #12)
> Though this would split the error handling above and below a ~100 line loop.
> Up to you, Philip.

Yeah, I prefer to keep the error handling localised and not split, so I won’t modify that patch further.

Thanks for the reviews, both of you. I’ll push these all shortly.
Comment 15 Philip Withnall 2018-02-28 17:40:51 UTC
All pushed to master. I don't think any of them are particularly worth backporting to glib-2-54.

Attachment 369026 [details] pushed as 85f3bc1 - gnetworkmonitornetlink: Fix potential GMainContext issue
Attachment 369027 [details] pushed as 23fad11 - gnetworkmonitornetlink: Use a coarser-grained timer for dumps
Attachment 369028 [details] pushed as 88bf493 - gnetworkmonitornetlink: Refactor some code to reduce duplication
Attachment 369029 [details] pushed as 183c846 - gnetworkmonitornetlink: Fix memory leaks on error paths
Attachment 369030 [details] pushed as 61a8998 - gnetworkmonitornetlink: Fix a memory leak in unusual circumstances
Attachment 369031 [details] pushed as 35d4c7f - gnetworkmonitornm: Fix some minor GVariant memory leaks
Attachment 369032 [details] pushed as 8266238 - gnetworkmonitornm: Use g_strv_contains() rather than reinventing it