Skip to content

lwip_sock: clean-up connection handling without a sock.#12932

Merged
miri64 merged 3 commits intoRIOT-OS:masterfrom
miri64:lwip_sock/fix/sock-cleanup
Jan 9, 2020
Merged

lwip_sock: clean-up connection handling without a sock.#12932
miri64 merged 3 commits intoRIOT-OS:masterfrom
miri64:lwip_sock/fix/sock-cleanup

Conversation

@miri64
Copy link
Copy Markdown
Member

@miri64 miri64 commented Dec 12, 2019

Contribution description

This is an enhanced version of #12927. In addition to the fix there, it removes the unnecessary additional indirection of conn as either the sock is provided with sock_*_send() or not. In the first case the indirection is not necessary, and in the second we need to delete the created conn within lwip_sock_send() anyway (as introduced in #12927, so returning it makes no sense.

It also adds a check for the call to lwip_sock_send() by the respective protocol callers, so that sock is not dereferenced when it is NULL

Testing procedure

lwip_sock tests should still compile and run for both IPv4 and IPv6 and both network layer protocols combined:

for prot in ip udp tcp; do
    LWIP_IPV4=1 make -C tests/lwip_sock_${prot}/ flash test
    LWIP_IPV6=1 make -C tests/lwip_sock_${prot} flash test
    LWIP_IPV4=1 LWIP_IPV6=1 make -C tests/lwip_sock_${prot} flash test
done
Either the sock is provided with `sock_*_send()` or not. In the first

case the indirection is not necessary, and in the second we need to
delete the created conn within lwip_sock_send() anyway, so returning
it makes no sense.

Issues/PRs references

Contains a modified version of #12927.
Fixes #12859.

Either the sock is provided with `sock_*_send()` or not. In the first
case the indirection is not necessary, and in the second we need to
delete the created `conn` within `lwip_sock_send()` anyway, so returning
it makes no sense.
@miri64 miri64 added Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) Area: network Area: Networking Area: pkg Area: External package ports Type: cleanup The issue proposes a clean-up / The PR cleans-up parts of the codebase / documentation CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR CI: run tests If set, CI server will run tests on hardware for the labeled PR labels Dec 12, 2019
@miri64 miri64 added this to the Release 2020.01 milestone Dec 12, 2019
@miri64 miri64 requested a review from benpicco December 12, 2019 10:28
@miri64
Copy link
Copy Markdown
Member Author

miri64 commented Dec 12, 2019

Just noticed a simpler check for when to delete the netconn. Let me know what you think.

@wosym
Copy link
Copy Markdown
Member

wosym commented Dec 12, 2019

I agree that these changes make it a lot cleaner and easier to grasp.
However, you now always delete the conn. Won't this break functions such as udv_recv(), because they rely on this open connection?

@miri64
Copy link
Copy Markdown
Member Author

miri64 commented Dec 12, 2019

Oops, that wasn't my attention... The problem with last minute changes ;-).

@wosym
Copy link
Copy Markdown
Member

wosym commented Dec 12, 2019

Looks good now! Clever way of using the tmp as a universal connection, and later on using the conn again to check if tmp needs to be freed or not, removing the need for an extra flag (like I did in my PR).

@benpicco
Copy link
Copy Markdown
Contributor

@wosym does this work for you?

@wosym
Copy link
Copy Markdown
Member

wosym commented Dec 13, 2019

@benpicco It seems to work, yes.

  • tests/lwip:
    • udp send --> able to send over 1000 messages without memory issues
    • ip send --> able to send over 1000 messages without memory issues
  • tests/lwip_sock_ip --> all tests succeed
  • tests/lwip_sock_udp --> all tests succeed

@benpicco benpicco added the Reviewed: 3-testing The PR was tested according to the maintainer guidelines label Dec 13, 2019
Copy link
Copy Markdown
Contributor

@benpicco benpicco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good too.
@miri64 go ahead and squash.

@miri64
Copy link
Copy Markdown
Member Author

miri64 commented Dec 13, 2019

Squashed from a hotel room with blocked SSH access...

@miri64
Copy link
Copy Markdown
Member Author

miri64 commented Dec 13, 2019

Mhhh let's try this again.

@miri64
Copy link
Copy Markdown
Member Author

miri64 commented Dec 13, 2019

Can somebody confirm that worked? Git tells me the branch is up-to-date, but I don't see the squash commits in the GitHub interface...

@miri64
Copy link
Copy Markdown
Member Author

miri64 commented Dec 13, 2019

$ git remote -v
origin	git@github.com:miri64/RIOT.git (fetch)
origin	git@github.com:miri64/RIOT.git (push)
upstream	git://github.com/RIOT-OS/RIOT.git (fetch)
upstream	git://github.com/RIOT-OS/RIOT.git (push)
git show --decorate
commit 49a8f64c62b933d9936675ee56acc71e889a0249 (HEAD -> lwip_sock/fix/sock-cleanup, origin/lwip_sock/fix/sock-cleanup)
Author: Wouter Symons <wsymons@nalys-group.com>
Date:   Wed Dec 11 16:14:52 2019 +0100

    pkg/lwip: fix memory issue when sending with empty sock

diff --git a/pkg/lwip/contrib/sock/lwip_sock.c b/pkg/lwip/contrib/sock/lwip_sock.c
index f236c078f..de38335a2 100644
--- a/pkg/lwip/contrib/sock/lwip_sock.c
+++ b/pkg/lwip/contrib/sock/lwip_sock.c
@@ -564,6 +564,9 @@ ssize_t lwip_sock_send(struct netconn *conn, const void *data, size_t len,
             break;
     }
     netbuf_delete(buf);
+    if (conn == NULL) {
+        netconn_delete(tmp);
+    }
     return res;
 }
$ git show --decorate
commit 49a8f64c62b933d9936675ee56acc71e889a0249 (HEAD -> lwip_sock/fix/sock-cleanup, origin/lwip_sock/fix/sock-cleanup)
Author: Wouter Symons <wsymons@nalys-group.com>
Date:   Wed Dec 11 16:14:52 2019 +0100

    pkg/lwip: fix memory issue when sending with empty sock

diff --git a/pkg/lwip/contrib/sock/lwip_sock.c b/pkg/lwip/contrib/sock/lwip_sock.c
index f236c078f..de38335a2 100644
--- a/pkg/lwip/contrib/sock/lwip_sock.c
+++ b/pkg/lwip/contrib/sock/lwip_sock.c
@@ -564,6 +564,9 @@ ssize_t lwip_sock_send(struct netconn *conn, const void *data, size_t len,
             break;
     }
     netbuf_delete(buf);
+    if (conn == NULL) {
+        netconn_delete(tmp);
+    }
     return res;
 }

@miri64
Copy link
Copy Markdown
Member Author

miri64 commented Dec 13, 2019

The branch even points to the correct commit... crappy hotel wifi is crappy or something is seriously wrong with GitHub...

@miri64 miri64 added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Dec 15, 2019
@miri64 miri64 added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Jan 9, 2020
@miri64 miri64 merged commit 87d00ab into RIOT-OS:master Jan 9, 2020
@miri64 miri64 deleted the lwip_sock/fix/sock-cleanup branch January 9, 2020 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: network Area: Networking Area: pkg Area: External package ports CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR CI: run tests If set, CI server will run tests on hardware for the labeled PR Reviewed: 3-testing The PR was tested according to the maintainer guidelines Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) Type: cleanup The issue proposes a clean-up / The PR cleans-up parts of the codebase / documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

lwip: multiple sock_udp_send() return ENOMEM. (possible memory leak?)

3 participants