Skip to content

Fix network usage - #433

Draft
gperciva wants to merge 8 commits into
masterfrom
fix-network-usage
Draft

Fix network usage#433
gperciva wants to merge 8 commits into
masterfrom
fix-network-usage

Conversation

@gperciva

@gperciva gperciva commented Aug 5, 2026

Copy link
Copy Markdown
Member

No description provided.

Reported by:	Claude Sonnet 5
Bug bounty:	$30 (3 * $10 "harmless")
If close() failed, we would re-enter drop() and then call
network_read_cancel() on an already-cancelled cookie.  This fixes it by:
- setting the cookie to NULL
- ensuring that we remove the node from the list, even if close() fails.

Reported by:	Claude Sonnet 5
Bug bounty:	$10 ("harmless")
The bare free(A) ignored A->conn_cookies.

Reported by:	Claude Sonnet 5
Bug bounty:	$10 ("harmless")
@gperciva

gperciva commented Aug 5, 2026

Copy link
Copy Markdown
Member Author

This follows up to Tarsnap/libcperciva#543.

network_accept_cancel(A->accept_cookie);
err3:
free(A);
simple_server_shutdown(A);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this safe to call on all possible event-loop exits?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Haven't seen a problem yet, but I'll return to checking it tomorrow.

Also, set a cancelled cookie to NULL.  This isn't strictly necessary;
it's is part of the clean-up so we'll be freeing the whole struct soon
anyway, but it looked a bit questionable.

Reported by:	Claude Sonnet 5
Bug bounty:	$11 ("harmless" + "style")
The original design of the server was to quit on any type of failure;
it's intended for single-machine testing where something like ECONNRESET
is unlikely.

However, we might as well relax that assumption, and keep the server
going even if a client is misbehaving.

Reported by:	Claude Sonnet 5
Bug bounty:	$10 ("harmless")
@gperciva

Copy link
Copy Markdown
Member Author

Revised with 2 new commits, although I'd like to change the order before the final merge. In particular, move the do not re-launch any accept in shutdown earlier so it's the first nc-server update. (The don't quit server will stay at the end.)

Comment thread tests/nc-server/simple_server.c Outdated
A->accept_cookie = NULL;
}

/* Stop drop() -> conndied() -> doaccept() from arming a new accept. */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd put this above the /* Cancel any further attempts. */ block; we're single-threaded so there isn't actually any race, but it's cleaner in principal to say "don't restart this, and stop it now" rather than "stop it now and don't restart it".

@cperciva

Copy link
Copy Markdown
Member

s/it's is part/it's part/.

@cperciva

Copy link
Copy Markdown
Member

What's the "NRC" in "network_read_cancel(NRC)"?

Comment thread lib/proto/proto_pipe.c Outdated
*(P->status) = -1;

/* Inform the upstream that our status has changed. */
P->callback(P->cookie);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(P->callback)(P->cookie); is our normal style for this.

@cperciva

Copy link
Copy Markdown
Member

A few minor nits, but I think the code is right in concept now. Yes, please re-order commits to avoid introducing a bug and then fixing it.

@gperciva

Copy link
Copy Markdown
Member Author

What's the "NRC" in "network_read_cancel(NRC)"?

network_read_cookie.

I'll reword it.

… in shutdown

Also, set a cancelled cookie to NULL.  This isn't strictly necessary;
it's part of the clean-up so we'll be freeing the whole struct soon
anyway, but it looked a bit questionable.

Reported by:	Claude Sonnet 5
Bug bounty:	$11 ("harmless" + "style")
We assigned `node_ptr`, but only used it for the assert().

This should have been part of:
    2022-04-17 tests/simple_server: switch to LIST from queue.h
    7be2eca

Suggested by:	Claude Sonnet 5
@gperciva
gperciva marked this pull request as draft August 14, 2026 20:37
@gperciva

Copy link
Copy Markdown
Member Author

Next edition:

  • two REBASE commits to fix issues you mentioned. Also, I added (void) to the callbacks in err1 paths.
  • in the process of checking them, Claude suggested a readability improvement to simple_server_shutdown() which I liked.

I haven't re-ordered the commits yet, because that would spoil the attempt to only show changes since your previous review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants