At every visit, your aging mother spends half an hour griping about the too-spicy food in her assisted-living community. Whenever you and a friend go clothes shopping together, she whines about how fat she looks in everything she tries on. A coworker constantly carps that none of your other coworkers puts in as much effort as she does.

Being surrounded by such habitual complainers can drain your energy and pollute your positivity, leaving you frustrated, irritated, disheartened and depressed. But there are ways to avoid getting sucked into gripers’ toxic traps. What helps…

Be sympathetic—but set limits. Complainers often feel unhappy, lonely, criticized or misunderstood. Resolve to listen to their grievances as compassionately as you can, but only up to a certain point. Set a time limit of, say, three minutes… or let your own internal cues (a feeling of being trapped, a knot in your stomach) tell you when you’ve heard enough. At that point, call a halt to the complaining by saying courteously yet firmly, “You have my sympathy. Now, let’s please talk about something more pleasant.” If the complainer still won’t quit, excuse yourself from the conversation, walk away or hang up the phone. Don’t chide yourself for cutting off the person’s rant—you are not being callous, you are simply protecting your own well-being.

Resist the urge to try to solve other people’s problems or fix their poor attitudes. If a person has a legitimate complaint and you are in a position to help—for instance, by speaking to the chef at your mom’s assisted-living facility—it would be kind of you to do so. But recognize that for many grouches, bellyaching is an ingrained habit or part of their personality. Since they usually are not looking for practical solutions (as is clear when they repeatedly ignore your suggestions) and are not open to an attitude adjustment, no amount of cajoling or cheerleading on your part will make the slightest difference. Accept the fact that it is neither your responsibility nor within your power to make everyone see a brighter side of life. Save your breath to save your sanity.

Tailor your strategy depending on your relationship to the ranter. In casual encounters (for instance, with whiny shopkeepers or grumbling taxi drivers), simply complete your business as expeditiously as possible while distracting yourself from their complaints by thinking your own thoughts. With a complaint-prone coworker, the goal is to avoid confrontations that could heighten conflict—so be polite but keep your distance, refusing to get sucked into a gripe-fest that could end up reflecting poorly on you. With a close friend or loved one, you’ll want to offer more compassion, of course. But if complaining is really out of hand, consistently shift the conversation to anything the person said that is pleasant… and then gently add that when people are suffering so much, they often can feel better if they consult a mental health professional.

Seek out positive-spirited companions. To help offset the stress of naysayers you cannot avoid, surround yourself with plenty of yeasayers—cheerful people who share and support your own glass-half-full outlook on life.

Consider whether you have a codependence on complainers. If you still seem to attract more than your share of bellyachers despite implementing the strategies above, ask yourself whether there’s an emotionally unhealthy underlying reason. Could you be using their faultfinding as a vicarious means of expressing discontent that you are too afraid to give voice to yourself? Do you secretly take satisfaction in the contrast between their constant grousing and your own image as Little Miss Sunshine? Do you feel the need to be a savior for others because it allows you to avoid facing your own sources of upset or pain? If the answer to any of these questions is yes, talk to a trusted friend or therapist about more appropriate ways to identify and deal with your own dissatisfaction.