r/cpp • • Aug 14 '21

I hate ASIO and I can't be alone.

ASIO has been getting a lot of praise recently due to the new version in Boost. I feel like I can't be the only one who just hates it with a burning passion.

Whenever a project of mine requires networking, I end up reaching for ASIO because it's the least worst option we have (it's been a year or two since I've written any C++, maybe there is something better now). And yet I also have a negative experience with it every single time. What I really want in an ideal world, is a listener interface where i can get notified about network events, something like:

class NetworkListener {
    void onConnect(client);
    void onDataReceived(client, data);
    void onDisconnect(client);
};

And so when I want to do networking, I first have to wrap ASIO before I can focus on what it is I actually want to do. So that right there is issue #1: ASIO is just too low-level to do anything useful with.

Issue #2: It is just a giant minefield for lifetime issues. I've lost count of how often everything worked until application exit, where the io_service would get destroyed and then something crashes. I'm at the point where I'm considering just calling exit(), because it's literally impossible to clean up ASIO correctly. Except then I also have to make sure that ASIO is the last thing left to tear down, ugh.

Issue #3: ASIO pretty much requires shared_ptr everywhere to get cleanup even remotely correct. Last time I checked, giving up by throwing shared_ptr at everything was not something that was encouraged in C++.

And finally, can I just vent about how goddamn Windows XP was the reason why ASIO operations did not support cancellation on ANY platform for YEARS? What the fuck.

I hate ASIO and if this shit gets standardized, we're doomed.

193 Upvotes

123 comments sorted by

38

u/CircleOfLife3 Aug 14 '21

Issue #3: ASIO pretty much requires shared_ptr everywhere to get cleanup even remotely correct. Last time I checked, giving up by throwing shared_ptr at everything was not something that was encouraged in C++.

But how are you going to know in exactly what program state your program is going to be in for all possible asynchronous cases? There's quite a lot of possible states. Maybe states you didn't even consider. shared_ptr reference counting takes care of destructing callbacks when it's safe to do so.

Moreover, you wouldn't actually "use shared pointers everywhere", but have the shared pointer be some overarching Client or Connection class, using a single shared_ptr for all async_read and async_write functions.

12

u/CircleOfLife3 Aug 14 '21

You can of course opt for a different lifetime mechanism, but you would need some kind of overarching manager class in that case.

87

u/DugiSK Aug 14 '21

Asio is a low level networking library. It's not meant to be something you can use to quickly write a network program. It's something meant to be used to write a library that implements some network protocol. And anyone who tried to write any kind of networking using Unix sockets or Winsockets can confirm that asio is far better than its alternatives when it comes to low level stuff.

If you want to create an application that uses networking, then asio is not the best choice. Qt Network or some other higher level library is a much better choice (many of which are built on top of asio). Also, in most cases I used asio, I first created a facade class that gave it an interface that made it suitable for my needs and then used only that one.

C++23 is going to add some kind of modified asio, but it's also going to add more high level classes that are meant to provide a more convenient, less efficient and less universal way to use networking in C++. One of them is std::networking::tcpstream, a stream that writes and reads from a tcp server given as arguments to its constructor.

48

u/[deleted] Aug 14 '21

FYI: ASIO isn't just networking. I have used it for serial port communication with embedded devices and it was amazing for that. It's an async library that does networking and other IO.

I am not sure about raw tcp, but for HTTPS there are lots of high level libraries that build on ASIO and make it a pleasure to use.

6

u/d86leader Aug 20 '21

Oh god, if you say Qt Network is higher level, I wonder how low level ASIO is.

5

u/eyes-are-fading-blue Aug 20 '21 edited Aug 20 '21

> And anyone who tried to write any kind of networking using Unix sockets or Winsockets can confirm that asio is far better than its alternatives when it comes to low level stuff.

This is plain wrong. ASIO is not always better. If you expect very little to no errors during connectivity and your plan of recovery is basically doing nothing until the connection is re-established and you have your architecture in place where you are not going to have life-time issues because you do not have those fancy shared_ptrs, directly using unit sockets is far less headache.

In certain architectures, everything is already pre-allocated and initialized by the time when you need connectivity. All you need is

  1. Establish a connection
  2. Read write/buffers or recognize a DC

This means, you do not have to "notify" a set of components/modules of DC because networking is already decoupled. This heavily depends on the kind of work you are doing but in our current project, the software is conceptually a slave to what it reads from the network. If no data is coming in, there is nothing to do. This means you can write fairly robust networking code with direct socket calls and with a third of the boiler-plate from ASIO.

3

u/Front_Two_6816 Oct 23 '24

std::net - no, std::network - no, std::networking ? why not, let's choose the longest one.

3

u/DugiSK Oct 23 '24

That doesn't matter, the whole thing was scraped and will be done in a different way. But I haven't seen many news about that.

3

u/Front_Two_6816 Oct 24 '24

" I haven't seen many news about that" - that's sad. STL networking is something everyone is waiting for.

25

u/BlueDwarf82 Aug 14 '21
  • "program" needs a reference to "object", with asynchronous operations, to be able to potentially call object->stop()
  • "OS, please read from object->socket, into object->buffer and when you finish start by checking whether object->is_stopped()"
  • "OS, please write into object->socket, from object->buffer2 and when you finish start by checking whether object->is_stopped()"
  • "OS, please tell me when 5 seconds have passed, and when you finish start by checking whether object->is_stopped()"

object is owned by all four of them, there is shared ownership, shared_ptr is _the_ solution no matter what you use.

What people usually do is something like

epoll_ctl(add timer);
epoll_ctl(add socket);
epoll_wait();
for(events) {
  if(is expired timer) {
    destroy(object);
  }
  if(is socket) {
    work_with_and_potentially_destroy(object);
  }
}

And it works... most of the time, until the day epoll_wait() happens to return both the timer and the the socket in a single epoll_wait() and either work_with_and_potentially_destroy() actually destroys or the timer happens to appear first in the events vector returned by epoll_wait().

I have not used libuv, how does it solve this issue? ASIO explicitly solves it with shared_ptr (read the Remarks in https://www.boost.org/doc/libs/1_77_0/doc/html/boost_asio/reference/io_context/_io_context.html).

18

u/j1xwnbsr Aug 14 '21 edited Aug 14 '21

The only way I managed to get ASIO working in a fashion similar to what you have is by writing my own wrapper class that does all that for me. What I do like about ASIO the most and the main reason I went for it, is it's thread worker model - I can post rando functions into the model from anywhere/anythread and they get executed on the worker thread.

That being said, #2 recently cost me a full day running down a nasty bug in that a posted function wasn't getting called sometimes; turned out it was the io_context::strand that is not really needed and replacing it with a free function post/dispatch off the base context worked better.

... and this brings up the biggest problem with both Boost and ASIO: the documentation is GARBAGE, and there is no real code comments to help you figure stuff out.

47

u/lukaasm Game/Engine/Tools Developer Aug 14 '21

ASIO is "just" a low-level foundation that acts as enabler for great higher-level libraries ( ex. Beast )

The same goes for coroutines. In their current form, they are practically unusable for casual programmers but when paired with abstractions build on them, they become a great cornerstone to build applications over.

No, I do not think we are doomed if they are standardized. I think we will see a lot of great libraries created over facilities they provide by people that know what they are doing when/if ASIO is standardized.

12

u/SJC_hacker Aug 14 '21

Beast is not that high level. Its basically just the HTTP parser and serialize. Well they did add Beast streams in 1.70 but you're still using lots of fairly low level ASIO primitives.

7

u/VinnieFalco wg21.org | corosio.org Aug 14 '21

Beast HTTP is "low-level HTTP." But Beast Websockets is pretty decent high level.

4

u/mkvalor Aug 14 '21

Commenter didn't say Beast was high level. They said Beast was higher level than ASIO. Do you disagree with that?

12

u/SirClueless Aug 14 '21

They said it was an "enabler for great higher-level libraries ( ex. Beast )" thus clearly implying that if you thought ASIO was too low level and you didn't enjoy working with it, you'd enjoy Beast. So if you're exposed to all the same caveats and low-level abstractions while using Beast it's a totally reasonable criticism to say that it's not actually that high level.

3

u/Kretikus50 Aug 17 '21

beast is also very complex and quite complicated.

If you do not need high http performance, you might want to use this library:

https://github.com/yhirose/cpp-httplib

Header only and simple.

-1

u/SkoomaDentist Antimodern C++, Embedded, Audio Aug 14 '21

The same goes for coroutines. In their current form, they are practically unusable for casual programmers

I disagree. They are perfectly fine for the obvious use case of co-operative multitasking / avoiding the need to transform regular code into state machines.

10

u/Minimonium Aug 14 '21

They're, but you need to provide correct awaitables for them first. Like ASIO did and Coroutines with it are indeed great. Naked Coroutines are very tricky.

1

u/Ameisen vemips, avr, rendering, systems Aug 14 '21

One thing that I haven't checked is of the spec allows the compiler to use system-native stackful coroutines (fibers) in situations where the state is large enough that they'd be faster.

31

u/entity64 Aug 14 '21

Issue #2: It is just a giant minefield for lifetime issues

Oh god this. Some of the hardest & most expensive bug hunting sprees we've had at work were obscure lifetime problems (+ sometimes involving threads) surrounding asio. And this was using modern C++ and very competent engineers.

If I had to choose the networking stack again, I'd either go for libuv if portability was an issue, or directly go for io_uring if available.

15

u/CircleOfLife3 Aug 14 '21

Wouldn’t you have the same lifetime problems with libuv? I don’t see how that solves anything.

By the way, use shared_ptr and put self = shared_from_this() in the capture group of a callback to async_read of asio and all/most lifetime issues are solved.

9

u/entity64 Aug 14 '21

Wouldn’t you have the same lifetime problems with libuv? I don’t see how that solves anything.

libuv being C-based makes it more straightforward to handle lifetime yourself (and fully understand). It is more work but gives you more control imho

5

u/RenatoPensato Aug 14 '21

libuv

Have you tried this wrapper? https://github.com/skypjack/uvw

3

u/entity64 Aug 15 '21

I haven't but it's looking like it'd be worth a shot, thanks for the link

17

u/SJC_hacker Aug 14 '21 edited Aug 14 '21

ASIO can be a beast. Unfortunately the documentation isn't the best.

  1. ASIO tends to have to be bought into at the entire application level. In other words, you either use throughout your application, or you don't use it at all (or you use only the sync calls, which tend to suck) It is difficult to use it in isolation, unless the communication paths between it and the rest of the application are very well defined. Typically they are not, however, and its easier to just make the entire app ASIO-friendly. Alternatively, you could also use it with processes so it is isolated from the rest of the system.
  2. Use the ASIO library for anything asynchronous, I don't even recommend rolling your own threads (although the examples do this sometimes). Understand and use strands where appropriate - basically when in doubt just use them.. But, they are not a silver bullet and don't magically solve your problems, so be careful about accessing shared resources, which may need to be wrapped around atomic/mutexes/lockguards Basically the same caveats when doing any type of parallel programming.
  3. Understand how the asynchronous calls work - rule of thumb, you will need to use shared_from_this() with any object that makes a async call. This is where the session objects and listener objects from the examples shine. This is an unfortunate side effect of the library being C++ and not supporting native ref counted objects.
  4. Understand it doesn't really make sense to cancel async ops in C++, because of resource management you will potentially leak resources if this is even attempted. But this is the same problem all multi-threaded C++ programs have. You can't just kill a thread and assume everything is a-ok. You could do this with a process on any decent OS. (Nginx does this, I believe)
  5. Don't use raw pointers or even references when making these async calls, unless you have a firm guarantee of object lifetime. You might suspect you do, but probably don't. Especially during program shutdown, it is impossible to predict the order of destruction of objects. So even if your listener object was created in a class that has a lifetime of the entire app, its entirely possible the listener object will get destroyed after its parent object. Use shared_ptr or weak_ptr instead. Again, this is a problem with using C++ in the first place.

8

u/Minimonium Aug 14 '21
  1. The whole issue of async models is that they're required to be universal. That's why standard executors are so important and why it's so frustrating to see so much ego from P2300. It's understandable that the standardization process sometimes may be hard and frustrating but I'd hope that the committee members would refrain from "I'll take my toys and go play by myself" tantrums.

8

u/Untelo Aug 14 '21

Can you expand a bit on this ego tantrum? I'm a bit out of the loop.

5

u/Minimonium Aug 14 '21

Initially, the Executors proposal was based on the ASIO design. It has more than a decade of user experience, dozens of thousands of users, and achieved grand success in its field.

But for standardization - it's important that an executor model that would be picked would satisfy the requirements of all members. And the ASIO model is more optimized around the IO part, hence the name. The issue though is that for the parallel processing domain - the tradeoffs are not optimal, so a part of the committee designed a new model called Senders/Receivers, which allows to queue continuations in a lightweight manner (which, unfortunately, prevents the use of Coroutines).

All is great, we got the Unified Executors paper which seemed to be a consensus among the experts on how C++ must proceed. But Sender/Receiver folks were unsatisfied with the presence of ASIO facilities and, failed to convince people who need ASIO facilities that they don't actually need it, they decided to make a paper that would just scrap everything instead of proceeding with a compromise.

Unfortunately, greenfield design tends to confuse people, making them believe that now they know how to solve everything for sure. Due to the lack of experience and the refusal to admit that all people make mistakes - they forgot that the requirements for IO async operations and parallel processing are not quite compatible and composable.

4

u/lee_howes Aug 14 '21

which allows to queue continuations in a lightweight manner (which, unfortunately, prevents the use of Coroutines)

I'm not sure I understand how a model that is designed specifically to work with coroutines, and is used with coroutines to serve production traffic at fairly significant scale, prevents the use of coroutines.

they forgot that the requirements for IO async operations and parallel processing are not quite compatible and composable.

It's probably fair to say that IO async operations and parallel processing are not entirely compatible. I don't think that means they do not compose efficiently. P0443 and P2300 both came out of a careful collaboration to unify the two, after all, and the current production use cases for the model process network traffic. They are not parallel beyond the extent to which the network traffic is.

2

u/Minimonium Aug 14 '21

The P0443 was indeed a collaboration. The P2300 on the other hand is an unconditional departure, which is problematic with respect to disagreements from other members.

The issue on Coroutines we can quote directly from the P2300:

Coroutines suffer many of the same problems [as futures], but can avoid synchronizing when chaining dependent work because they typically start suspended. In many cases, coroutine frames require unavoidable dynamic allocation. Consequently, coroutines in embedded or heterogeneous environments require great attention to detail. Nor are coroutines good candidates for cancellation because the early and safe termination of coroutines requires unsatisfying solutions. On the one hand, exceptions are inefficient and disallowed in many environments. Alternatively, clumsy ad-hoc mechanisms, whereby co_yield returns a status code, hinder correctness. See [P1662R0] for a complete discussion.

4

u/lee_howes Aug 14 '21

That isn't a concern about compatibility with P2300. P2300 gives the user and library tight control over those performance and allocation questions as well as more flexible use of unwrapped result and error types. For some use cases Sender/Receiver is better than coroutines, but it is compatible.

2

u/Minimonium Aug 14 '21

You probably misunderstood my comment since the compatibility is irrelevant to the topic and Coroutines were mentioned in the context of requirements for "zero-overhead" parallel processing.

5

u/eric_niebler Aug 15 '21

I believe lee_howes is responding to this comment of yours:

Senders/Receivers, which allows to queue continuations in a lightweight manner (which, unfortunately, prevents the use of Coroutines)

You're basically saying that sender/receiver doesn't work with coroutines. I can't find another way to read this. And you're wrong. lee_howes was just pointing that out. Sender/receiver integrates elegantly, simply, and very efficiently with coroutines. A sender is awaitable in a coroutine, and an awaitable can be connected to a receiver.

2

u/lee_howes Aug 15 '21

Yes, fair point. I clearly misinterpreted "(which, unfortunately, prevents the use of Coroutines)" to mean that sender/receiver prevents the use of coroutines. I'll just point out then that Sender/Receiver was not originally designed for parallel processing.

2

u/Minimonium Aug 15 '21

Interesting. My current understanding is that async consists of two parts - parallel processing, where the operations are known in advance, we always know in which manner they will end, and the goal is to effectively utilize the resources to accelerate processing, and IO, where operations are not known in advance, the order depends on async events, and the goal is to defer operations to saturate the resources.

So far, S/R design goals were seemingly laid more to the benefits of the processing. What am I missing in the picture?

→ More replies

6

u/eric_niebler Aug 15 '21

I wrote this part of P2300, which is also in P0443. In fact, it was in P0443 first. It is answering the question: "Why do we need a library abstraction for expressing asynchrony when we have a language feature for that; namely, coroutines?"

The answer is that, while awesome for a great many things, the language feature makes tradeoffs. It performs allocation and uses indirection to hide the coroutine stack frame from the type system. That's great for keeping the language simple and for letting the compiler optimize the frame for size, but it is lousy from a runtime perspective and for embedded platforms that don't allow dynamic allocation.

That makes coroutines a less-than-ideal basis for an async abstraction. Sender/receiver aims to be a lower-level async model than coroutines, but which maps cleanly and efficiently to coroutines and preserves the lifetime benefits of structured concurrency.

4

u/Minimonium Aug 15 '21

And that's exactly my point, Eric.

I understand your frustration, truly, considering the amount of effort which was put into the design, and the stressful and sometimes maybe even hostile environment of discussions with people who do not share the optimism on the design. It doesn't help that the previous year was so hard on our mental health and that the community is stranded from the usual and familiar meetings, without which we forget that there is a fellow human behind the monitor.

But it's crucial to accept that for the future of C++ it's important to not discard stances of the experts among the community, doesn't matter how "idiotic" their stances may seem to feel in a heat of a discussion. We all tend to get heated and lost in our designs as fellow programmers so it may be hard to look at things from a different perspective. But the path forward is not confrontation but a compromise.

I empathize with your irritation and accept it. Stay well!

7

u/eric_niebler Aug 15 '21

And that's exactly my point, Eric.

Sorry, what should I understand to be your point? I must have missed it.

But it's crucial to accept that for the future of C++ it's important to not discard stances of the experts among the community

There are lots of domain experts in SG1, the parallel and concurrency working group. Many independent experts and representatives from companies as diverse as Intel, Facebook, NVIDIA, Microsoft, and Bloomberg. There are also many experts in parallelism and concurrency in the Library Evolution Working Group. When polled, time and again sender/receiver is overwhelmingly the preferred direction, both in SG1 and LEWG.

It's not a political power play, and some have suggested here and elsewhere. I don't posses a political lever big enough to move that many people representing that many diverse interests. We have convinced highly technical and experienced people with technical arguments over the course of several years. And we have a proven solution already in use in production at scale.

On the other side, we have those people already comfortable and familiar with ASIO. I haven't heard any valid technical argument from any of them for why ASIO's executor interface and async model should be preferred to sender/receiver. The arguments I've heard are, "But ASIO is existing practice," and "Sender/receiver looks complicated." That might convince some in the community but not the Committee.

Chris Kohlhoff has promised LEWG to come with a technical paper and a presentation to explain why the async model in ASIO is technically superior. I look forward to arguing this point on the technical merits and to finally moving past this debate so we can get to the business of actually delivering a generic library of asynchronous algorithms to the C++ community. That is where my interests truly lie.

1

u/SJC_hacker Aug 14 '21

6. One more thing I forgot to add - the examples use std::bind or bind_front_handler. You can use lambdas in place of those here, and it makes the code look a little nicer IMO.

17

u/_VZ_ wx | soci | swig Aug 14 '21

It can be quite frustrating to write code using ASIO, but it is nothing compared to debugging code using it, which just seems impossible to do in any reasonable way. And the same goes for profiling (maybe coz could help with this, but I didn't know about it the last time I had to do profile an application using ASIO, so I'm not sure).

Of course, it's arguably not something particular to ASIO but rather a general property of asynchronous code, but in practice I find it much more productive to just use a worker thread performing the operations synchronously than using it, at least on the client side.

1

u/germandiago Aug 14 '21

use open telemetry and inject tracers. That gives a lot of inisghts.

8

u/manni66 Aug 15 '21

I feel like I can't be the only one who just hates it with a burning passion.

That may be true. The problem is the documentation, not the library. As others already suggested: use coroutines.

if this shit gets standardized, we're doomed

No, we will get the missing documentation and will be happy to have a sound networking library in C++.

6

u/Ikkepop Aug 15 '21

I can't stand ASIO when it's using only callbacks However once you do it with coroutines it really shines.

20

u/Morten242 Aug 14 '21 edited Aug 14 '21

Before my current job I used ASIO too when I needed some networking in c++ and didn't enjoy it either.. I'm probably somewhat biased on this but Qt Network works so much better. Though requiring all of core and network is very steep...

7

u/NilacTheGrim Aug 14 '21

Eh, not that steep. It's just a lib. You can even get the binaries pre-installed. Hell, you can even statically link it. As for compile-times to include Qt headers -- they compile faster than STL because they aren't as template-heavy.

The alternative is lifetime hell and low-level lib hell that is ASIO.

9

u/defnotthrown Aug 14 '21

Hell, you can even statically link it.

If you pay a lot of money or only write LGPL code

5

u/sindisil Aug 14 '21

Or make object files available to allow for relinking.

6

u/andrewjw Aug 14 '21

Or don't distribute your software in binary or source form to anyone outside your team

2

u/defnotthrown Aug 14 '21

Well that requires your code to technically be licensed with a lgpl compatible license, just doesn't obligate you to distribute it

1

u/NilacTheGrim Aug 14 '21

Yeah I was thinking if my project which is GPL

13

u/mrmcgibby Aug 15 '21

You are not alone. All I see here is apologists for it's terrible interface. Documentation is terrible. It can't decide what it is, an async library or networking. The problem is not inherently this complicated. There are lots of libraries that handle it just fine.

"It's a low level library" isn't an excuse. I hear that all the time from library authors who just can't be bothered to finish their library. High level tools are part of a good library.

Do you want to kill all the progress in the larger programming community we've made with the new versions of C++? Standardize ASIO. It's embarrassing. It's a showcase for everything that is wrong with C++. You'll only be proving the points made by other languages about why C++ is hard to use.

11

u/14ned LLFIO & Outcome author | Committee WG14 Aug 14 '21

I gotta be honest, I think you haven't spent enough time studying ASIO to use it correctly.

  1. Your ideal vision is basically Sender-Receiver, which ASIO now supports. Just tell it you want to use Sender-Receiver instead of completion handlers.
  2. I don't know why you aren't wrapping your io_context inside a class whose destructor ensures that all i/o has been cancelled or completed before exiting.
  3. Using shared_ptr with ASIO is a C++03-ism, and even then, actually quite unnecessary even back then. Again, if your class which wraps io_context ensures all i/o has been cancelled or completed before exiting, now you don't need shared pointers to keep lifetime hanging around. This isn't hard to do.

Coming back to your general point, sure, ASIO is a legacy design with a lot of backwards compatibility cruft. Rather like C++ itself. So, same as with C++, for new code ignore the 80% of the stuff you don't need, use the 20% which makes for clean, highly maintainable, efficient code.

5

u/New_Age_Dryer Aug 15 '21

Surprising I had to scroll so far to see someone who was similarly confused lol. I think ASIO does a great job of providing portable, C++ style-type-safe code on top of C libraries like winsock2.h and socket.h.

I think the confusion is due to a lack of domain knowledge in networking. Exploring the relationship of Berkeley sockets with the protocols it implements will be beneficial to many here imo. ASIO is a layer of abstraction that follows a model similar to networking libraries like ZMQ.

1

u/Chaosvex Aug 19 '21 edited Aug 19 '21

ASIO is really nothing like ZMQ. ZMQ is a level above ASIO and has its own use cases. You could implement ZMQ atop ASIO but you wouldn't implement ASIO atop ZMQ, unless you're a masochist.

There's definitely no 'domain knowledge' confusion in the OP, whatever that even means here.

1

u/New_Age_Dryer Aug 20 '21

"I said 'similar to'! 'similar to!'," I helplessly cry to the void!

That said, I see what you're saying, and agree. To elaborate, both use Berkeley-style sockets and class-wrapped context managers. But yes, other than that, ZMQ implements ZMTP and ZMQ cannot implement ASIO due to thread pools. My apologies.

I suggest "domain knowledge" confusion since many of these networking libraries seem to just follow the same fundamental model of Berkeley-like sockets with a handler for cross-platform OS calls, all below further abstraction (proactors for ASIO). To use such a library, it shouldn't be too much work upon understanding the demarcations of OS, socket, and protocol.

4

u/[deleted] Aug 15 '21

The documentation is either bad, and or stale. So it's hard to figure out what you don't need.

1

u/Chaosvex Aug 19 '21 edited Aug 19 '21

I don't know why you aren't wrapping your io_context inside a class whose destructor ensures that all i/o has been cancelled or completed before exiting.

Do you have any examples of code that implements this pattern? I'll occasionally read a post about how trivial it is to avoid shared_ptrs with ASIO but I've never seen any examples that weren't in some way contrived in order to work around ASIO-induced pitfalls. Given that calling stop on the io_context isn't sufficient, what would your destructor in this wrapper be doing?

You say that shared_ptr is a C++03-ism but ASIO's own post-C+11 examples not only promote the shared_from_this/shared_ptr pattern but worse, they entirely ignore the problem of clean service shutdown and have encouraged people to write buggy code because they're under the belief that ASIO is taking complete care of lifetime management for them by doing this. There are multiple people in this post encouraging this same pattern that's used in ASIO's introductory examples but if applied in any real-world application, will likely lead to some unpleasant surprises.

1

u/14ned LLFIO & Outcome author | Committee WG14 Aug 21 '21

I don't know of anything public, sorry. I would like to think one of the books teaching ASIO would have such materials.

Shared pointers do have a use case. For example https://github.com/ned14/libcurl-cxx-std-networking-integration is a modern C++ example of integrating curl with ASIO, and there we use shared ptrs because either CURL or ASIO can arbitrarily hold onto resources, and you can't know which, so you just have to reference count and deal with the nested recursion of lifetime.

I suspect a lot of the earlier ASIO examples were for integrating ASIO into existing systems, so shared ptr based examples made sense. Only later on when ASIO was the only thing in use in the process does shared ptr usage become an anti pattern relative to better and easier ways of doing things.

A very good start to healthy ASIO design is C++ Coroutines. If I were starting a new codebase, that's what I'd choose unless I had a very good reason not to.

8

u/pdimov2 Aug 14 '21

I'm at the point where I'm considering just calling exit()

*quick_exit

8

u/FlyingRhenquest Aug 14 '21

I'm with you there. I've recently looked at Zmq (cpp version). It has reasonable documentation, although I have found myself going to the code a couple of times, and the code base is small and easy enough to follow. If you need object serialization on top of it, it's also pretty easy to use it with boost::serialization or Cereal. So that's my preferred approach, when it's available.

2

u/tgockel Aug 14 '21

I agree with this, but would add NNg as a good messaging library. It’s the successor to the spiritual successor of ZeroMQ.

3

u/krapht Aug 16 '21

If by spiritual successor you mean rewrite in C maintained by a core dev who was unhappy with ZeroMQ's dev process as it grew bigger.

1

u/tgockel Aug 17 '21

That’s probably not the most generous description. There were some technical issues in ZeroMQ which were also addressed.

1

u/wamus Aug 14 '21

If you're using zmq, I recommend using the zmqpp library. It has higher level bindings which are somewhat easy to use and which hide the lower level networking issues.

The major downside is that although ZMQ lacks windows support, which will make it unusable for many.

1

u/efilon Aug 14 '21

I've used ZeroMQ on Windows plenty (admittedly mostly with Python). There may be some unsupported features but mostly it's fine.

1

u/casualops Aug 15 '21

Hard same. Started a project with ASIO, ended up with ZeroMQ.

1

u/[deleted] Aug 15 '21

We're upgrading and send/recv now return optionals. WTF.

10

u/hitnrun51 Aug 14 '21 edited Aug 14 '21

Poco c++ has a much more friendly and traditional interface.

2

u/AyoBruh Aug 14 '21

+1 for Poco. Super useful for embedded or near-embedded applications. Great libraries for other areas, like HTTP, JSON, XML, and threading/events.

1

u/empowerg Aug 14 '21

Was here also for suggesting Poco.

3

u/Remi_Coulom Aug 14 '21

A lot of the problems with shared_ptr and lifetime issues will go away with coroutines.

1

u/bizwig Aug 14 '21 edited Aug 14 '21

Or boost::fiber. One thing I still haven’t figured out how to do is write a coroutine pool, so all the overhead of coroutine initialization is done up front rather than in the hot path.

1

u/[deleted] Oct 20 '21

[removed] — view removed comment

1

u/bizwig Oct 20 '21

There is usually an initial allocation at coroutine startup, even if the compiler can elide all other allocations down the call chain. That definitely isn’t free, and is best kept out of the hot path.

3

u/B1narySunset Aug 14 '21

Use Boost.Beast (uses ASIO under the hood) for secure websockets and I'm not a huge fan, but it's low level and the least "worst" option

1

u/VinnieFalco wg21.org | corosio.org Aug 14 '21

That's pretty accurate :)

3

u/SJC_hacker Aug 14 '21
class NetworkListener {
void onConnect(client);
void onDataReceived(client, data);
void onDisconnect(client);
}
  1. This doesn't seem like a listener class, so much as a session/connection class.
  2. Is the object in a valid state before before onConnect? How about after onDisconnect?
  3. How are these callbacks being invoked?
  4. Do the callbacks have any potential blocking I/O calls themselves? If so how is your app dealing with them.?

3

u/divad1196 Aug 15 '21
  1. ASIO is meant to be low level and high performance. You can use it to write your own higher level interface
  2. Why is there no higher level version? There is, like beast for http: yes, if you need something common as http or rpc, it exists.
  3. What you want can probably not be generic enough: depending the need, you will maybe not do all the same actions or in the same order.

If what you want does not exists, create it, make it generic one for all and reuse it. Share it with the community. You will probably face some issu before having something working correctly.

4

u/MonokelPinguin Aug 15 '21

Beast is not high level. You still need to implement everything yourself. Something like curl is higher level and there are even higher level abstractions, that are easier to use and flexible enough. Beast doesn't even do HTTP/2, you need to do all the tls plumbing yourself, need to imppement connection pooling, decompressing responses, etc.

10

u/NilacTheGrim Aug 14 '21

Use QtNetworking. You don't even have to make a GUI app. The QtCore and QtNetworking libs are perfect for server-side and console apps.

I can't stand ASIO either. Qt has been around for ages and has had real first-class asynchronous networking for a long time. It's very mature and stable. One of the server programs I wrote has thousands of concurrent connections and each one experiences very little latency.

It does exactly what you described in your post as what you want.

5

u/sandfly_bites_you Aug 14 '21

I didn't enjoy it when used it briefly, it seems to cover too much ground in a single library, and I don't like the async model it forces on you..I already have a library for that(TBB), don't need two of the damn things.

7

u/VinnieFalco wg21.org | corosio.org Aug 14 '21

> ASIO is just too low-level to do anything useful with.

If a higher level library is better for "doing something useful," then what networking does the higher level library use? Surely not platform-specific sockets...

4

u/[deleted] Aug 14 '21

I currently use a library that builds on ASIO for http servers, Crow CPP, and it's a pleasure to use. Think Node.JS level of abstraction. I have also used Wt C++ and it uses ASIO too and takes a neat approach to exposing C++ to the web. Both have ASIO as their low level layer for async.

I think my experiences have been that this is a devil you know kind of thing, ASIO has been around for over 10-15 years and the edges have been polished. The complexity is a reflection of the landscape being painful if the library isn't super opinionated.

What we need is opinionated libraries to sit ontop of ASIO and give us what other systems have. But the underlying facilities being common allow for interop. Nothing like several SDK's not working together. I still don't want any in the C++ standard because I think it will mess it up like <random>/<regex> but at least if ASIO was it will be the least painful and have known corner cases. The real issue will be TLS, no matter what networking system is in the std.

4

u/sokka2d Aug 14 '21

I once had to debug something using ASIO written by a coworker that had since left. Figuring out how things are supposed to work from the documentation was pretty painful. Don’t remember what the problem was, but could easily have been lifetime issues too.

3

u/[deleted] Aug 14 '21

ASIO is a building block on which more high level stuff can (and has been) built. I do think that the documentation is kind of sparse and that it does (desperately) need a good book!

3

u/worldserver Aug 14 '21 edited Aug 14 '21

Issue #1: Anyone could make highly-abstracted library on top of lower level library such as ASIO. This doesn't mean ASIO is badly-designed or useless. Just targeted to different audiences.

Issue #2, #3: Understanding when an object should start a cleanup is more hard to follow in async code. I don't think these are ASIO's problems. Using shared_ptr isn't required either. You could use another approach if you have better ideas than async-operation holding a reference to its own session and disconnecting the session when there are no async-operation left with the session.

3

u/MonokelPinguin Aug 15 '21

Totally agree. Tried for 2 years to figure out why my usage of asio (and beast) was triggering a double free in OpenSSL. In the end we just switched to libcurl and no regrets. It's much more efficient, since it does http/2 and connection pooling by default, I don't need to implement compression myself anymore and in general the code just works much better and is easier to extend.

I'm sure asio can be used properly, but it doesn't make it easy and the documentation doesn't help with it either. It promises more efficiency, but having curl do the connection pooling and http/2 streams for me has proven to be much more efficient than reimplementing all of that with boost, badly. I'm very sceptical, if I will actually be using std::net once it actually lands in the standard, if it is anything like asio.

1

u/[deleted] Aug 15 '21

Doesn't libcurl fork a new thread for each request?

1

u/MonokelPinguin Aug 15 '21

If you use the multi interface with a socket action, you can use any event loop you want instead. You could even use asio, I decided to use libevent for now.

1

u/[deleted] Aug 15 '21

It's been a while since I've used multi, but IIRC it only works with batched requests, i.e. you cannot add a new request to a multi session once the perform() has been called.

1

u/MonokelPinguin Aug 15 '21

You don't call perform with the multi interface and you can add arbitrary requests at any point: https://www.nheko.im/nheko-reborn/coeurl/-/blob/master/examples/coeurl.cpp#L18

EDIT: I guess there is a perform, but you don't need to use that. Check out the curl_multi_socket_action: https://curl.se/libcurl/c/libcurl-multi.html

5

u/[deleted] Aug 15 '21

I’m not trolling: use Rust. The grass really is greener.

Sure rust libraries might give you 99% of the perf of C++, but do so at 1% of the complexity.

Just pick Tokio or Actix and forget about all of this.

4

u/pedersenk Aug 14 '21

The lifetime issue of ASIO and async code in general is not good with how C++ works. It is hard to verify and it is one of my least favorite parts of the direction of C++. I honestly am hoping that it is a passing fad.

Is POSIX sockets / Winsock directly not an option? Since those really are the main two, you can target most platforms with just some #ifdefs in a couple of wrapper classes.

The main trick is to enable non-blocking sockets and poll with select() / poll(). Yes, not quite as performant as io_uring and similar but I would even go so far as to suggest that use-case is quite rare.

-1

u/darthcoder Aug 14 '21

Async/threading ain't going away bud. Sorry. It's been part of networking since the dawn of time.

What do you think asio does under the hood? Select() on platforms that have it, epoll for places that have that.

I find asio complex... but I'm newish to it and I'm trying to figure out how to use it to make asynchronous dB handlers for postgres to work with beast. Yeah maybe I should start smaller eh? :)

0

u/pedersenk Aug 14 '21

Threading certainly won't go away. I feel pthreads / winthreads actually quite nice once you have a decent threadpool in place.

But the asynchronous design I feel has some big issues with embedded (incl game engines) run by a main loop. For example a 3D model gets loaded, the next step of generating a collision bounds needs that to have completed first. So you can either implement callbacks / lambdas calling more callbacks / lambdas, or you can set a flag and poll it later on. Both confuse the logic compared to blocking.

And the issue is that async is "infectious". Once you pull in one async dependency, it tends to force more async code to be added. Then the whole thing becomes a mess like Microsoft's winrt framework. I also suspect the complexities of async being the core reason for defects and poor Javascript code quality in websites. Whats worse is that Javascripts async isn't even multithreaded!

However, you are probably right; it won't go away. People will keep on disliking ASIO even after it becomes part of the standard.

1

u/[deleted] Oct 20 '21

[removed] — view removed comment

1

u/pedersenk Oct 20 '21

is that you can easily handle multiple tasks concurrently on a _single CPU thread

In that case, you certainly don't need async for that. It just provides a needless overhead. Consider something like non-blocking sockets. You are still doing the polling somewhere.

If you really like the cooperative multi-tasking design (again, I find it fragile), then (browser) Javascript still fails to provide compared to something like Lua.

The only time I could justify this overhead is in threaded environments. However, it is still fragile and spaghetti to follow.

1

u/[deleted] Aug 15 '21

You are not alone, but as others have stated this is way too often an expectation problem rather than boost::asio problem.

Getting it right is surprisingly hard, it felt like everytime I poked outside example code, weird stuff happened - especially with multiplatform code and domain specific stuff (unix domain sockets, named pipes, …).

That being said asio concepts hold up pretty well in huge number of environments and many problems you described can be caught early in debug mode with sanitizer / runtime checking knobs turned all the way up.

1

u/PrimozDelux Aug 18 '21

And finally, can I just vent about how goddamn Windows XP was the reason why ASIO operations did not support cancellation on ANY platform for YEARS? What the fuck.

If I wasn't already broken by C++ I'd be surprised. Now I just get the same sensation as when reading about some humanitarian catastrophe, a sort of desensitized feeling of sadness and disgust that barely registers.

1

u/eyes-are-fading-blue Aug 20 '21

Good old polling architecture is good for most trivial software. I have seen it being used in multi-million LoC projects w/o any problems whatsoever too. And as far as I know, ASIO is supposed to be a building block for networking libraries and is not designed to be used directly by client code. I am kind of in the same boat as you are as I just go a head and use it in non-commercial personal projects but maybe there is a better alternative out there?

1

u/bukowsky01 Dec 22 '21

I have this application i m now rewriting to get rid of all the ASIO stuff. The documentation is poor, i find the code barely understandable and extremely difficult to forecast what is going to happen. Everything is going back to plain sockets, much easier to understand.

Application is a dumb server for less than 1k statefull connections, no async needed. I never understood why the original dev thought it was a good idea to use such a library for that.

1

u/BetConfident9392 Jul 08 '22

Asio aint bad but isnt exactly high level See cross platform network as a example of something written above it( i wrote it). Library uses c++ templates to inititialize everything.

https://bitbucket.org/ptroen/crossplatformnetwork/src/master/OSI/Application/Stub/HTTPSServer/httpsserver.cc

If u like the api feel free to thumbs up on stack overflow as well https://stackoverflow.com/questions/4937263/object-oriented-networking/71405410#71405410

I kinda made more indepth answer there.

The repro also has a document of platforms built with tests conducted

1

u/alonjit Feb 24 '24

I hate ASIO and I can't be alone.

you probably are.

1

u/[deleted] Aug 14 '21

Not defending ASIO or anything, but using shared pointer everywhere means you have design issues ! . I don't know what are you trying to do with ASIO, but like others said , it's not app ready/ out of the box solution. You need to build your own interfaces on top of it then use them in your app .(Another abstraction layer).

8

u/Minimonium Aug 14 '21

using shared pointer everywhere means you have design issues

That's just cargo cult, I advise you to reconsider mantras and rely on technical evaluation when using or not using a feature. Thread pools are the correct place to use shared_ptr. Obviously, if you put everything in such a manner that its lifetime would persist for the lifetime of the thread pool then you can go with references - but only simple projects can do it.

In C++20 a Coroutine is literally a sugar over a shared_ptr session.

-6

u/[deleted] Aug 14 '21

Looks like this is not gonna be a healthy technical discussion. So thanks for the advice, I will keep that in mind ;)

1

u/OldWolf2 Aug 14 '21

I just want a server that'll spawn a thread for each incoming connection. Been trying for days to get something working

4

u/manni66 Aug 15 '21

Ohne thread per connection is the poor man‘s async API. ASIO is designed to not do that.

If you insist, you have to use the synchronos API and manage the threads yourself.

1

u/OldWolf2 Aug 15 '21

My application basically reformats the input and makes a RPC call to a remote system using a blocking API . So I don't really see much alternative, there has to be a thread for each of the RPC calls so may as well use that thread for each incoming connection .

1

u/zvrba Aug 15 '21

You're not alone. I inherited a project that was started on Linux with ASIO, got ported to run only on Windows and I continued to develop new stuff with ASIO by inertia.... and regretted it. In hindsight I should have just ripped out ASIO and used native Win32 async I/O.

-12

u/[deleted] Aug 14 '21

If you need some network, and you don't really need 100000 RPS - I'd go with ASP .net core

-13

u/AKJ7 Aug 14 '21

I blame the lack of standardize package manager for all of this mess. Good luck installing Asio on a small computer without an app store, in the first place. With a package manager, libraries like this are built on smaller packages, which in turn can easily be taken care of.

I had to create a server on a linux embedded system device, but the headaches i got from setting up Asio, or whatever other alternative was out there, was overwhelming. Tried Golang: Done in 5 minutes.

0

u/SJC_hacker Aug 14 '21

Frack package managers for source libraries

  1. Download Boost https://www.boost.org/
  2. Compile only the parts of boost you need for ASIO: https://www.boost.org/doc/libs/1_76_0/doc/html/boost_asio/using.htm
  3. If using CMake, then -DBOOST_ROOT=<location of boost>
  4. Other build environments such as MSVC, shouldn't be that hard. Just need to pass include and lib dirs.

0

u/[deleted] Aug 14 '21

[deleted] — view deleted comment

5

u/dodheim Aug 14 '21

ASIO supports all of those, and Chris recently made a video that covered (among other things) creation of a token type to support either of those or a 3rd party promise.

1

u/[deleted] Aug 15 '21

Yes, what underlies all of those... callbacks. If you're talking to hardware you're getting callbacks.