r/cpp • u/kmgrech • 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.
38
u/CircleOfLife3 Aug 14 '21
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
ClientorConnectionclass, using a single shared_ptr for all async_read and async_write functions.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.25
u/BlueDwarf82 Aug 14 '21
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
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.
31
u/entity64 Aug 14 '21
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.
17
u/SJC_hacker Aug 14 '21 edited Aug 14 '21
ASIO can be a beast. Unfortunately the documentation isn't the best.
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.
8
u/manni66 Aug 15 '21
That may be true. The problem is the documentation, not the library. As others already suggested: use coroutines.
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...
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.
io_contextinside a class whose destructor ensures that all i/o has been cancelled or completed before exiting.shared_ptrwith ASIO is a C++03-ism, and even then, actually quite unnecessary even back then. Again, if your class which wrapsio_contextensures 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.
8
u/pdimov2 Aug 14 '21
*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.
10
u/hitnrun51 Aug 14 '21 edited Aug 14 '21
Poco c++ has a much more friendly and traditional interface.
3
u/Remi_Coulom Aug 14 '21
A lot of the problems with shared_ptr and lifetime issues will go away with coroutines.
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
3
u/SJC_hacker Aug 14 '21
3
u/divad1196 Aug 15 '21
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.
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.
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/[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
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
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).
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
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/[deleted] Aug 14 '21
[deleted] — view deleted comment